在Objective-C中解析JSON中的JSON [英] Parsing JSON Within JSON in Objective-C

查看:113
本文介绍了在Objective-C中解析JSON中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试存储我从以下请求获得的JSON内的JSON ...

I'm trying to store the JSON that is within the JSON i get from the following request...

NSURL *URL = [NSURL URLWithString:@"http://www.demo.com/server/rest/login?username=admin&password=123"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"GET"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                    completionHandler:
                          ^(NSData *data, NSURLResponse *response, NSError *error) {

                              if (error) {
                                  // Handle error...
                                  return;
                              }

                              if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                  NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
                                  NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
                              }

                              NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                              NSLog(@"Response Body:\n%@\n", body);
                          }];
[task resume];

正文获得的结果JSON如下所示,您可以看到JSON中有一个JSON,如何在 NSDictionary 中存储 JSON,因为您可以看到JSON在引号之间。

The resulting JSON obtain from body is the following and as you can see there is a JSON within the JSON, how can I store that JSON in a NSDictionary as you can see that JSON is between quotation marks.

    [
        {
            tag: "login",
            status: true,
            data: 
            "
                {
                    "userID":1,
                    "name":"John",
                    "lastName":"Titor",
                    "username":"jtitor01",
                    "userEmail":"jtitor01@gmail.com",
                    "age":28,
                }
            "
        }
    ]


推荐答案

你现实中拥有的东西:
Classic JSON,其中有一个字符串代表一个JSON。

What you have in reality: Classic JSON, where inside there is a String "representing" a JSON.

所以,因为我们可以这样做:

NSData< => NSString

NSArray / NSDictionary< => JSON NSData

我们只需要根据我们的数据类型在它们之间切换已经。

So, since we can do:
NSData <=> NSString
NSArray/NSDictionary <=> JSON NSData
We just have to switch between them according to the kind of data we have.

NSArray *topLevelJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
NSString *lowLevelString = [[topLevelJSON firstObject] objectForKey:@"data"]; 
NSData *lowLevelData = [lowLevelString dataUsingEncoding:NSUTF8StringEncoding]; 
NSDictionary *final = [NSJSONSerialization JSONObjectWithData:lowLevelData options:0 error:nil];

这篇关于在Objective-C中解析JSON中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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