将JSON解码为NSArray或NSDictionary

编程入门 行业动态 更新时间:2024-10-25 16:18:43
本文介绍了将JSON解码为NSArray或NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望对下面的JSON数据进行解码:

I hope to decode the JSON data below:

{ "content": [ { "1":"a", "2":"b", "3":"c", "4":"d", "mark":"yes" } ] }

不确定是否将其放入NSArray或NSDictionary

Not sure if put it in NSArray or NSDictionary

欢迎发表评论

推荐答案

您使用的是哪个iOS版本?在iOS 5中,您具有NSJSONSerialization类来解析JSON数据,如果您需要定位较旧的iOS或MAC OSX,则应使用第三方库,例如SBJSON.发布的字符串将是NSDictionary,其中包含一个带有一个字典的数组.使用键@"content"

which iOS version are you using? in iOS 5 you have the NSJSONSerialization class to parse JSON data, if you need to target older iOSs or MAC OSX you should use third parties lib such as SBJSON. The string posted will be a NSDictionary with an array with one dictionary. The array will be accessible using the key @"content"

在代码中:

NSString * jsonString = @"blblblblblb"; NSStringEncoding encoding; NSData * jsonData = [jsonString dataUsingEncoding:encoding]; NSError * error=nil; NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

在SWIFT 2.0中:

In SWIFT 2.0:

let jsonString = "blblblblblb" let encoding = NSUTF8StringEncoding let jsonData = jsonString.dataUsingEncoding(encoding) guard let jData = jsonData else {return} do { let parsedData = try NSJSONSerialization.JSONObjectWithData(jData, options: []) } catch let error { print("json error: \(error)") }

[更新] NSJSONSerialization类也可用于10.7我的评论不正确.

[UPDATE] The NSJSONSerialization class is also available for 10.7 my comment wasn't correct.

更多推荐

将JSON解码为NSArray或NSDictionary

本文发布于:2023-08-03 08:09:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1285503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   NSArray   NSDictionary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!