Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

ios - Parsing JSON Within JSON in Objective-C

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
", (long)[(NSHTTPURLResponse *)response statusCode]);
                                  NSLog(@"Response HTTP Headers:
%@
", [(NSHTTPURLResponse *)response allHeaderFields]);
                              }

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

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":"[email protected]",
                    "age":28,
                }
            "
        }
    ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...