xcode 如何在 Swift 3.1 中从 jsonObject 获取 JSONArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45137469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the JSONArray from jsonObject in Swift 3.1
提问by Noorul
{
"status": true,
"status_code": 1,
"content": [
{
"cat_id": "3",
"cat_name": "Food",
"cat_parentid": "2"
},
{
"cat_id": "4",
"cat_name": "Entertainment",
"cat_parentid": "2"
},
{
"cat_id": "5",
"cat_name": "Cars",
"cat_parentid": "2"
},
{
"cat_id": "12",
"cat_name": "Personal Care",
"cat_parentid": "2"
}
],
"message": "Success"
}
UPDATE
更新
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
completion((json as? AnyObject)!) //here completion callback will return the jsonObject to my UIViewController.
}
} catch let error {
print(error.localizedDescription)
}
this is my JSONObject. I am very new to the swift. how to get the content JSONArray and further process in swift.? Anybody can help me? Help will be appreciated.
这是我的 JSONObject。我对 swift 很陌生。如何快速获取内容 JSONArray 并进一步处理。?任何人都可以帮助我吗?帮助将不胜感激。
回答by vadian
This code checks if the status is true
, gets the array for key content
and prints all values in the array.
此代码检查状态是否为true
,获取键的数组content
并打印数组中的所有值。
The array is clearly [[String:String]]
so cast the object to this specific type.
数组显然是[[String:String]]
这样将对象强制转换为这种特定类型的。
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
if let status = json["status"] as? Bool, status == true {
if let content = json["content"] as? [[String:String]] {
for category in content {
let id = category["cat_id"]
let name = category["cat_name"]
let parentId = category["cat_parentid"]
print(id , name, parentId)
}
}
}
}
} catch let error {
print(error.localizedDescription)
}
PS: As always, never use .mutableContainers
in Swift. It's meaningless
PS:一如既往,永远不要.mutableContainers
在 Swift 中使用。毫无意义
回答by Itachi Uchiha
Check whether your json has content array
检查你的 json 是否有内容数组
if let content = json["content"] as? [Dictionary<String, AnyObject>] {
print(content) // it will give you content array
}
回答by firstinq
Get content array like this:
像这样获取内容数组:
let allContent = json["content"] as? [[String: Any]]
Full sample:
完整样本:
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
if let allContent = json["content"] as? [[String: Any]] {
for content in allContent {
let catId = content["cat_id"] as? String
let catName = content["cat_name"] as? String
let catParentId = content["cat_parentid"] as? String
print(">> catid=" + catId!)
print(">> catName=" + catName!)
print(">> catparentID=" + catParentId!)
}
}
}
} catch let error {
print(error.localizedDescription)
}
回答by Narayana
You can access like below
您可以像下面这样访问
if let filePath = Bundle.main.path(forResource: "sample", ofType: "json"), let data = FileManager().contents(atPath: filePath) {
do {
let dicRes = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
let contentArray = dicRes?["content"]
print("contentArray == \(contentArray)")
} catch {
}
}
回答by Dhiru
you can extract you data by providing key
您可以通过提供密钥来提取数据
if let array = result["content"] as? Array<AnyObject> {
print(arry)
}
回答by vp2698
let content = dict.objectForKey("content")! as NSArray
Then you can get json of single object for parsing by
然后你可以得到单个对象的json进行解析
for var cat in content
{
print(cat)
}
回答by Imad Ali
Another alternative way, by using the library.
另一种替代方法,通过使用库。
First, import JSON library for Swift - SwiftyJSONand use the code:
首先,为 Swift 导入 JSON 库 - SwiftyJSON并使用代码:
import SwiftyJSON
let json = JSON(<jsonObject data>)
let contentArray: Array<JSON> = json["content"].arrayValue
Library Integration
If you're using cocoapods then use this pod:
库集成
如果你使用 cocoapods,那么使用这个 pod:
pod 'SwiftyJSON'
pod 'SwiftyJSON'
OR else just drag SwiftyJSON.swift
to the project tree.
或者只是拖到SwiftyJSON.swift
项目树上。