AWSSET
AWSSET

Reputation: 417

How to parse the JSON string from invoked adapter in Worklight

We have a JSON file in the server. We were able to retrieve the JSON file, but apparently we could not get the items in the nested JSON.

Here is the snippet of our code:

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

Below is our JSON file structure:

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

Upvotes: 0

Views: 90

Answers (1)

cnandreu
cnandreu

Reputation: 5111

You can use valueForKeyPath: after you have an NSDictionary on the client.

For example, pseudocode:

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

I assume your NSDictionary referenced by allData has the contacts NSArray in the results.contacts key path. If that's not the case, modify according to the structure of your NSDictionary.

Upvotes: 2

Related Questions