2012-07-09 57 views
0

大家好我是一個新的iphone開發者。我收到了我所需的URL的響應,如下所示,我需要獲得"CategoryID":1的名稱和"CategoryID":2的名稱都在單獨的數組集合中。如何實現獲取iphone中的JSON對象的代碼?謝謝你得到響應 代碼:如何實現獲取iPhone中的JSON對象的代碼?

theXML = [[NSString alloc] initWithBytes: [RoutData mutableBytes] length:[RoutData length] encoding:NSUTF8StringEncoding]; 
NSLog(@"---->>>>>>-->>>:%@",theXML); 

響應:

LocationTracking[1807:f803] ---->>>>>>-->>>:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getGearLockerCategoriesResponse xmlns="http://tempuri.org/"><getGearLockerCategoriesResult><status>SUCCESS</status><errorType /><errorMessage /><jsonString>[{"CategoryID":1,"ItemCount":1,"level":1,"Name":"Boots","Description":"Boots descrition","childs":[{"CategoryID":2,"ItemCount":1,"level":2,"Name":"Cold Water","Description":"Cold Water Description","childs":[]}]}]</jsonString><serverTimestamp>63477507673796</serverTimestamp></getGearLockerCategoriesResult></getGearLockerCategoriesResponse></soap:Body></soap:Envelope> 

<jsonString> 

[{"CategoryID":1,"ItemCount":1,"level":1,"Name":"Boots","Description":"Boots 

descrition","childs":[{"CategoryID":2,"ItemCount":1,"level":2,"Name":"Cold 

Water","Description":"Cold Water Description","childs":[]}]}] 

</jsonString> 

<serverTimestamp>63477390375625</serverTimestamp> 

</DiveTravelerResponse> 
+1

獲取NSMutableDictionary中的所有數據,然後設置條件。然後在條件基礎上添加NSMutableArray然後從數組中獲取值。 – Ayaz 2012-07-09 06:17:04

回答

0

NSJSONSerialization。如果您的JSON位於名爲'str'的NSString中:

NSError *jsonError = nil; 
NSArray *rootObjectArray = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError]; 
if (rootObjectArray) 
{ 
    /* you have a foundation object */ 
} 
else 
{ 
    /* something went wrong */ 
    NSLog(@"jsonError: %@", [jsonError localizedDescription]); 
} 
+0

您好朋友我已經使用您的給定答案我的請求代碼,但問題是當我實現的代碼是像 NSError * jsonError = nil; NSArray * rootObjectArray = [NSJSONSerialization JSONObjectWithData:[theXML dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];但我得到的答覆像2012-07-10 07:59:35.020 LocationTracking [1807:f803] --- ****** --- :(null)如何解決問題 – user1510990 2012-07-10 12:56:46