Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing a complex JSON result (array of dictionaries) with TouchJSON
    text
    copied!<p>I did a few tests with TouchJSON last night and it worked pretty well in general for simple cases. I'm using the following code to read some JSON content from a file, and deserialize it:</p> <pre><code>NSString *jsonString = [[NSString alloc] initWithContentsOfFile:@"data.json"]; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding]; NSError *error = nil; NSDictionary *items = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&amp;error]; NSLog(@"total items: %d", [items count]); NSLog(@"error: %@", [error localizedDescription]); </code></pre> <p>That works fine if I have a very simple JSON object in the file (i.e. a dictionary):</p> <pre><code>{"id": "54354", "name": "boohoo"} </code></pre> <p>This way I was able to get access to the array of values, as I wanted to get the item based on its index within the list:</p> <pre><code>NSArray *items_list = [items allValues]; NSString *name = [items_list objectAtIndex:1]; </code></pre> <p><em>(I understand that I could have fetched the name with the dictionary API)</em></p> <p>Now I would like to deserialize a semi-complex JSON string, which represents an array of dictionaries. An example of such a JSON string is below:</p> <pre><code>[{"id": "123456", "name": "touchjson"}, {"id": "3456", "name": "bleh"}] </code></pre> <p>When I try to run the same code above against this new content in the data.json file, I don't get any results back. My NSLog() call says "total items: 0", and no error is coming back in the NSError object.</p> <p>Any clues on what is going on? I'm completely lost on what to do, as there isn't much documentation available for TouchJSON, and much less usage examples.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload