在Objective-C中parsingJSON中的JSON

我试图存储JSON中的JSON,我从以下请求中获得…

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,我怎样才能将这个 JSON存储在一个NSDictionary ,你可以看到JSON是在引号之间。

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

你现实中有什么:经典的JSON,里面有一个string“代表”一个JSON。

所以,因为我们可以这样做:
NSData <=> NSString
NSArray / NSDictionary <=> JSON NSData
我们只需根据我们所拥有的数据types在它们之间切换。

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

你应该使用这行代码

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

这将有助于

尝试这个

 - (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"]); } }