2013-02-04 64 views
0

如何處理json對象與字符串中的子對象。下面是一個例子Objective C NSJSONSerialization如何解析子json

[{「_id」:「1」,「Title」:「Pineapple」,「Description」:「Dole Pineapple」,「Icon」:「icon.png」,「Actions」 「ACTION_PHOTO」:「coupon.png」,「ACTION_LINK」:「google.com」}}]

如何解析第二個json「Actions」?

回答

3

這裏有一個字典數組(1條目),其中頂級字典中的一個條目也是一個字典。所以你可能有這樣的解析它:

NSError *e = nil; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: &e]; 

if (jsonArray) { 
    NSDictionary *dictActions; 
    for (NSDictionary *dict in jsonArray) { 
     dictActions = [dict objectForKey:@"Actions"]; 
     NSLog(@"The action link is: %@", [dictActions [email protected]"ACTION_LINK"]); 
    } 
} else { 
    NSLog(@"Error parsing JSON: %@", [e localizedDescription]); 
} 
+0

謝謝,幫助噸! –

+0

沒有問題。如果它適合你,請接受答案。 – Joel