2014-09-24 17 views
0

我們在服務器中有一個JSON文件。我們能夠檢索JSON文件,但顯然我們無法獲取嵌套JSON中的項目。如何在Worklight中從調用的適配器中解析JSON字符串

這裏是我們的代碼片段:

- (void)onSuccess:(WLResponse *)response{ 
    NSLog(@"Connection Success: %@",response); 
    NSString *resultText; 

    if ([response responseText] != nil) { 
     resultText = [response responseText]; 

     NSDictionary *allData = [response getResponseJson]; 
     NSDictionary *resultData = allData[@"result"]; 
     ... 
     } 
} 

下面是我們的JSON文件結構:

{"contacts":[ 
    { 
     "name":"name 1", 
     "address":"address 1" 
    }, 
    { 
     "name":"name 2", 
     "address":"address 2" 
    }, 
    { 
     "name":"name 3", 
     "address":"address 3" 
    } 
]} 

回答

2

可以使用valueForKeyPath:您在客戶端上有一個NSDictionary之後。

例如,僞代碼:

NSArray* contacts = [allData valueForKeyPath:@"result.contacts"]; 
NSString* firstContactName = contacts[0][@"name"]; 

我假設你通過allData引用的NSDictionary已在results.contacts關鍵路徑接觸的NSArray。如果情況並非如此,請根據您的NSDictionary的結構進行修改。

+0

嗨,我很抱歉的回覆。我們試圖在我們的代碼中實現這一點。不幸的是,我們得到這個錯誤:* [錯誤]工作燈: - [WLClient onInvokeProcedureFailure:userInfo:]:932 * – AWSSET 2014-09-25 06:40:37

+0

您是否解決了此問題WLClient onInvokeProcedureFailure:userInfo:因爲我面對相同 – MichealSmith 2015-12-08 08:30:36