ios - 如何将JSON字符串提取到数组?

标签 ios objective-c json

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

最佳答案

您不必导入任何框架,因为它使用核心 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);

这会将您的对象注销到控制台。它将是一个 NSArray 或一个 NSDictionary 并将包含...的实例

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

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

关于ios - 如何将JSON字符串提取到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16940101/

相关文章:

iphone - UIImageView 带有大图像。问题

ios - 由于依赖冲突,Parse/FacebookUtils 和 FacebookCore 无法工作。

ios - 如何在 Xcode 6 的设计时做自定义字体?

iOS 6 - 以编程方式触发 Unwind Segue 不执行任何操作

objective-c - 在 tabBarView 中管理套接字流

json - 如何使用 Elm 对集合进行 JSON.Encode?

java - 当我在模型类中添加 Handler 对象时,GSON 对象序列化中断

ios - 如何在模糊效果之上有一个 UIPageView?

objective-c - UIAlertController:supportedInterfaceOrientations 被递归调用

mysql - 在包含 json 的文本字段中搜索