2015-10-15 32 views
0

我想存儲的JSON是JSON我從下面的請求得到內...解析JSON在JSON在Objective-C

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":"[email protected]", 
        "age":28, 
       } 
      " 
     } 
    ] 
+2

'的NSArray *頂層= [NSJSONSerialization JSONObjectWithData:數據選項:0錯誤:無]; NSString * lowLevel = [[topLevel firstObject] objectForKey:@「data」]; NSData * lowLevelData = [lowLevel dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary * final = [NSJSONSerialization JSONObjectWithData:lowLevelData options:0 error:nil];'或類似的東西可以做到這一點。 – Larme

+0

是的,它的工作,謝謝! – xzrudy

回答

2

你有什麼現實: 經典JSON,其中裏面有一個字符串「代表」一JSON。

所以,既然我們可以這樣做:
NSData的< =>的NSString
的NSArray/NSDictionary的< => JSON的NSData
我們只是根據種類的數據,我們在它們之間進行切換。

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

您應使用此行代碼

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

將help.thanks

+0

@bayadi - 你檢查網址是否有效,檢查一次,然後提交你的答案 –

0

嘗試此

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