如何将JSON字符串提取为数组?

3

使用HTTP GET请求,我获得了这个JSON字符串:

hello
[
    {
        "upid":"1",
        "uptitle":"test",
        "uptype":"test",
        "upsize":"1234",
        "upname":"test",
        "upuid":"1",
        "uplocation":"1",
        "uptimestamp":"2013-04-11 09:25:40"
    },
    {
        "upid":"17",
        "uptitle":"test",
        "uptype":"video\/mov",
        "upsize":"2232",
        "upname":"testing",
        "upuid":"1",
        "uplocation":"",
        "uptimestamp":"2013-04-12 11:47:20"
    }
]

如何以以下格式输出它:
upid:1
uptitle:test
uptype:test
upsize:1234
upname:test
upuid:1
uplocation:1
uptimestamp:2013-04-11 09:25:40

upid:17
uptitle:test
uptype:video\/mov
upsize:2232
upname:testing
upuid:1
uplocation:
uptimestamp:2013-04-12 11:47:20

这是我的代码:
NSMutableURLRequest *GETrequest = [[NSMutableURLRequest alloc] init];
    [GETrequest setURL:[NSURL URLWithString:@"http:someip/upload/?id=1&command=alluseruploads&uid=1"]];
    [GETrequest setHTTPMethod:@"GET"];
    [GETrequest setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

// And to send and receive an answer, use a NSURLResponse:
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:GETrequest returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);


// converting string to data
NSData *jsonData = [theReply dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"dic = %@", jsonData);


NSError* error;

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSLog(@"%@", jsonObject);

当记录theReply时,控制台会显示什么? - Fogmeister
2个回答

11

由于使用了核心iOS框架,因此您无需导入任何框架。

如果您有一个名为jsonStringNSString,那么您可以这样做...

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

// you can skip this step by just using the NSData that you get from the http request instead of converting it to a string.

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:&localError];

NSLog(@"%@", jsonObject);

这将在控制台中记录您的对象。它将是一个NSArrayNSDictionary,并且将包含实例...

NSDate
NSDictionary
NSArray
NSNumber
NSString
你可以迭代这个对象来获取数据。
好的,使用你的代码...
NSURLResponse *response;
NSData *getData = [NSURLConnection sendSynchronousRequest:getRequest returningResponse:&response error:nil];

NSError* error;

id jsonObject = [NSJSONSerialization JSONObjectWithData:getData options: NSJSONReadingMutableContainers error:&error];

NSLog(@"%@", jsonObject);
NSLog(@"%@", error);

这应该会创建您的对象。您不需要将其转换为字符串,然后再转回数据。


添加到我的答案中。同时,整理一下你的变量名 :) - Fogmeister
为了避免神秘数字,您应该将“option”参数更改为更易读的人类可读形式:NSJSONReadingMutableContainers - Jakub
@Fogmeister: 谢谢,已经修正变量名 :) 您能否提供带有for循环的完整代码,以便我可以获得输出?因为我仍然得到jsonObject为(null),并且错误是:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x2fc030 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} - Robertas Uldukis
问题在于你的JSON格式不正确。如果你在问题中打印的以“hello”开头的字符串是你接收到的内容,那么它就不是有效的JSON。 - Fogmeister
编辑了原帖以格式化JSON。除了开头的“hello”之外,一切看起来都很好。只需删除“hello”即可。 - Fogmeister
@Fogmeister 你真是个天才... 排序好了,一切都正常工作!!!原来是 PHP 代码中的愚蠢打印命令!!! - Robertas Uldukis

-1
使用SBJson库。它非常容易。

1
我总是选择使用NSJSONSerialization,因为它不依赖于第三方框架,并且比SBJSON快得多。 - Fogmeister
@Fogmeister 是的,但 SBJSon 为我节省了很多编码时间。 - Durgaprasad
我在哪里可以找到我的情况的示例?我已经导入了那个库。 - Robertas Uldukis
2
SBJSON 节省时间?为什么?NSJSONSerialization 总是一行代码!http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html - Jakub
我说它为我节省了时间。我已经使用它很多天了。它对我很有效。在下一个项目中,我将尝试使用NSJSONParser。 - Durgaprasad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接