简体   繁体   English

解析JSON目标c

[英]Parsing JSON objective c

I'm initiating a URL connection and my script is returning the following JSON: 我正在启动URL连接,我的脚本返回以下JSON:

( 1, { "id" = <-ID Here->; hash = <-Hash Here->; }, ( ), ( ) ) (1,{“id”= <-ID Here->; hash = <-Hash Here->;},(),())

All the examples I've seen so far seem to have "ids" or identifiers before respective arrays/dictionaries. 到目前为止,我看到的所有示例似乎都在相应的数组/字典之前有“ID”或标识符。 Despite searching around, I couldn't find a way to parse this. 尽管四处寻找,我找不到解析这个问题的方法。 (ie I need to get the first boolean value, the id, the hash, and then the arrays as well (which are empty as of now)). (即我需要得到第一个布尔值,id,哈希,然后是数组(现在是空的))。

Sorry if I'm missing something-- I'm new to parsing JSON in Obj-C.Thanks for the help. 对不起,如果我遗漏了一些东西 - 我是在Obj-C中解析JSON的新手。谢谢你的帮助。

The feature I use is built in with the Cocoa libraries: the NSJSONSerialization class. 我使用的功能是使用Cocoa库构建的:NSJSONSerialization类。 It provides methods for parsing JSON into a graph, and encoding a graph into JSON. 它提供了将JSON解析为图形并将图形编码为JSON的方法。 The rules are similar to plists (ie basic types plus arrays and dictionaries). 规则类似于plists(即基本类型加上数组和字典)。

If you have NSData (which you can easily get from a string), you do like this: 如果您有NSData(可以从字符串中轻松获取),您可以这样做:

NSArray *yourJSONAsObjectGraph = [NSJSONSerialization JSONObjectWithData:yourNSData options:nil error:&err];

Then, with your data above, objectAtIndex:0 would be an NSNumber that you can call boolValue on, objectAtIndex:1 will be an NSDictionary that you can call objectForKey:@"id" (and @"hash"), etc. 然后,使用上面的数据,objectAtIndex:0将是一个可以调用boolValue的NSNumber,objectAtIndex:1将是一个NSDictionary,你可以调用objectForKey:@"id" (和@“hash”)等。

See How to use JSON in Objective-C . 请参见如何在Objective-C中使用JSON If you didn't hear about it yet, have a look at SBJSON . 如果您还没有听说过,请查看SBJSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM