如何将JSON字符串提取到数组? [英] How to extract JSON string to array?

查看:157
本文介绍了如何将JSON字符串提取到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用http get request我确实得到了这个json字符串:

Using http get request i did get this json string:

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"
    }
]

我如何以格式获得输出:

How i can get it output in format:

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);


推荐答案

您不必导入任何框架,因为这个使用核心iOS框架。

You don't have to import any frameworks as this uses a core iOS framework.

如果你有 NSString 名为 jsonString 然后你可以这样做......

If you have an NSString called jsonString then you can do something like this...

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);

这会将您的对象注销到控制台。它将是 NSArray NSDictionary ,并将包含...

This will log out to the console your object. It will either be an NSArray or an NSDictionary and will contain instances of...

NSDate
NSDictionary
NSArray
NSNumber
NSString

然后你可以迭代这个对象来获取数据。

You can then iterate this object to get the data.

好的,使用你的代码.. 。

OK, using your code...

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);

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

This should create your object. You don't need to convert to string and then back to data.

这篇关于如何将JSON字符串提取到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆