1

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

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,
                }
            "
        }
    ]
2
  • 2
    NSArray *topLevel = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSString *lowLevel = [[topLevel firstObject] objectForKey:@"data"]; NSData *lowLevelData = [lowLevel dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *final = [NSJSONSerialization JSONObjectWithData:lowLevelData options:0 error:nil]; or something like that could do the trick. Commented Oct 15, 2015 at 12:57
  • Yeah it worked, thanks!! Commented Oct 15, 2015 at 13:12

3 Answers 3

2

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];
Sign up to request clarification or add additional context in comments.

Comments

0

You should use this line of code

NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:&errorJson];

it will help.thanks

1 Comment

@bayadi -- are you checked the URL is valid or not, check once and then submit your answer
0

Try this

- (void)loadDetails {
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request
                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                _allVehicleLocationsArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                [self afterCompleteDoThis];
            }] resume];
}

- (void)afterCompleteDoThis {
    for (NSDictionary *vehicleDict in _allVehicleLocationsArray) {
        NSLog(@" PPP %@" , [vehicleDict valueForKey:@"vehicleType"]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.