JSONKit 解析 json 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5290407/
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
JSONKit parsing json string
提问by happy_iphone_developer
How to use JSONKit to deserialize a json stringinto dictionary? I couldn't find any examples.
如何使用 JSONKit 将json 字符串反序列化为字典?我找不到任何例子。
回答by Caleb
Use the NSString interface that comes with JSONKit. For example:
使用 JSONKit 自带的 NSString 接口。例如:
NSDictionary *deserializedData = [jsonString objectFromJSONString];
You'll either need to know in advance what sort of container you're going to get from the JSON data, or else test the object you get back to figure out its type. (You can use -isKindOfClass: for this.) Most of the time, you already have an expectation of what's in the JSON data, so you know to expect a dictionary or an array, but it's still a good idea to check the class of the object you get back.
您要么需要提前知道将从 JSON 数据中获取哪种类型的容器,要么测试返回的对象以确定其类型。(您可以为此使用 -isKindOfClass:。)大多数情况下,您已经对 JSON 数据中的内容有了预期,因此您知道需要字典或数组,但检查类仍然是一个好主意你回来的对象。

