如何在 iOS Swift 中获取 json 值 4
How to Fetch json value in iOS Swift 4
{ "pages": 1, "posts": [ { "id": 2345, "categories": [],
"tags": [], "author": {
"id": 1,
"slug": "admin",
"url": "",
"description": "" }, "comments": [], "custom_fields": {
"slide_template": [
"default"
],
"request_status": [
"Pending Review"
],
"branch": [
"Branch B"
],
"assigned_to": [
"Managing Director"
],
"category": [
"Equipment"
],
"priority": [
"High"
] } },
这是我的 swift 编码
直到 "custom_fields" 我可以获取值,但在那之后我无法获取
请任何人帮助我
let url = NSURL(string: urlForCharts)
do{
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let data = data {
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let postArray = jsonObj!.value(forKey: "posts") as? NSArray {
for status in postArray{
if let tagsDict = status as? NSDictionary {
if let tagsArray = tagsDict.value(forKey: "custom_fields") as? NSArray {
for statusArray in tagsArray{
if let statusDict = statusArray as? NSDictionary {
if let status = statusDict.value(forKey: "request_status") {
self.ticketStatus.append((status as? String)!)
self.ticketStatus = self.ticketStatus.sorted(by: <)
您的 custom_fields
是 Dictionary
的类型,而不是 NSArray
。
"custom_fields": {
"slide_template": ["default"],
"request_status": ["Pending Review"],
"branch": ["Branch B"],
"assigned_to": ["Managing Director"],
"category": ["Equipment"],
"priority": ["High"]
}
所以它应该是这样的:
if let tagsDictionary = tagsDict.value(forKey: "custom_fields") as? NSDictionary {
if let slide_template = tagsDictionary.value(forKey: "slide_template") as? NSArray{
}
if let request_status = tagsDictionary.value(forKey: "request_status") as? NSArray{
if let resquestStatus = request_status.first as? String{
//append this value to your array
}
}
}
试试这个
if let tagsArray = tagsDict.value(forKey: "custom_fields") as? NSDictionary {
}
{ "pages": 1, "posts": [ { "id": 2345, "categories": [], "tags": [], "author": { "id": 1, "slug": "admin", "url": "", "description": "" }, "comments": [], "custom_fields": { "slide_template": [ "default" ], "request_status": [ "Pending Review" ], "branch": [ "Branch B" ], "assigned_to": [ "Managing Director" ], "category": [ "Equipment" ], "priority": [ "High" ] } },
这是我的 swift 编码 直到 "custom_fields" 我可以获取值,但在那之后我无法获取 请任何人帮助我
let url = NSURL(string: urlForCharts)
do{
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let data = data {
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let postArray = jsonObj!.value(forKey: "posts") as? NSArray {
for status in postArray{
if let tagsDict = status as? NSDictionary {
if let tagsArray = tagsDict.value(forKey: "custom_fields") as? NSArray {
for statusArray in tagsArray{
if let statusDict = statusArray as? NSDictionary {
if let status = statusDict.value(forKey: "request_status") {
self.ticketStatus.append((status as? String)!)
self.ticketStatus = self.ticketStatus.sorted(by: <)
您的 custom_fields
是 Dictionary
的类型,而不是 NSArray
。
"custom_fields": {
"slide_template": ["default"],
"request_status": ["Pending Review"],
"branch": ["Branch B"],
"assigned_to": ["Managing Director"],
"category": ["Equipment"],
"priority": ["High"]
}
所以它应该是这样的:
if let tagsDictionary = tagsDict.value(forKey: "custom_fields") as? NSDictionary {
if let slide_template = tagsDictionary.value(forKey: "slide_template") as? NSArray{
}
if let request_status = tagsDictionary.value(forKey: "request_status") as? NSArray{
if let resquestStatus = request_status.first as? String{
//append this value to your array
}
}
}
试试这个
if let tagsArray = tagsDict.value(forKey: "custom_fields") as? NSDictionary {
}