views:

7404

answers:

6

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:

NSString *jsonString = [[NSString alloc] initWithContentsOfFile:@"data.json"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
NSDictionary *items = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSLog(@"total items: %d", [items count]);
NSLog(@"error: %@", [error localizedDescription]);

That works fine if I have a very simple JSON object in the file (i.e. a dictionary):

{"id": "54354", "name": "boohoo"}

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:

NSArray *items_list = [items allValues];
NSString *name = [items_list objectAtIndex:1];

(I understand that I could have fetched the name with the dictionary API)

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:

[{"id": "123456", "name": "touchjson"}, {"id": "3456", "name": "bleh"}]

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.

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.

+4  A: 

This isn't an answer, but a pointer to a different framework:

http://code.google.com/p/json-framework/

I've been using it quite a bit lately, serializing and de-serializing complex data structures from third-party services such as Google Local and between my own Objective-C and Perl code with absolutely no problems. Not to mention that the API is ridiculously easy to deal with.

Good luck!

rpj
Thanks, I was able to figure out how to deserialize that complex JSON string with json-framework.
jpm
I've been using this framework as well for complex json structures and have yet to run in to any problems
seanalltogether
A: 

At it's heart JSON deals with objects, your code to de-serialize should be as follows

{"objects":[{"id": "123456", "name": "touchjson"}, {"id": "3456", "name": "bleh"}]}

which does work with the latest checkout.

wisequark
A good json framework should be able to handle either an object or an array as the high level container for the data structure
seanalltogether
@sean - that's not true. Technically JSON with an array as a high level container is NOT legal JSON.
schwa
schwa, says who?http://www.ietf.org/rfc/rfc4627.txt?number=4627"A JSON text is a serialized object or array.JSON-text = object / array""5. GeneratorsA JSON generator produces JSON text. The resulting text MUST strictly conform to the JSON grammar."That doc even has top level array exampl
seanalltogether
See http://robubu.com/?p=24 - "Safe JSON" is JSON with a top level dictionary. There are vulnerabilities with top level arrays.
schwa
http://directwebremoting.org/blog/joe/2007/03/06/json_is_not_as_safe_as_people_think_it_is_part_2.html There are vulnerabilities with Objects as well, but this is a browser sandbox issue, not a JSON issue. 'Legal' JSON is array or object.
seanalltogether
+5  A: 

I'm the author of TouchJSON.

Your outermost object should be a dictionary and NOT an array. Anything other than a dictionary is not legal. If you have to have an array as the outermost object then use the method (which is technically deprecated, but isn't going any where soon)

- (id)deserialize:(NSData *)inData error:(NSError **)outError;

See: http://www.json.com/json-schema-proposal/ for more information abotu what is and is not legal JSON.

schwa
I appreciate your response to my question, first of all. I was surprised to hear that the outermost object cannot be an array, as I have been doing this type of JSON result for a long time now, and most times things work out just fine with other libraries. Continuing on a separate comment...
jpm
According to the JSON RFC created by Douglas Crockford himself (http://www.ietf.org/rfc/rfc4627.txt), arrays as outermost objects are perfectly valid. See Page 7/8 for an actual example in there.
jpm
See http://robubu.com/?p=24 for more info. Top level arrays ought to be avoided.Regardless TouchJSON can process them - see the API i listed above.
schwa
Well, can we at least agree that it is perfectly "legal" to have arrays as the outermost object? What that post is describing is a browser javascript interpreter vulnerability. Arrays are perfectly legal, but maybe just not officially supported by your library.
jpm
In most languages it is also perfectly legal to do something like `++i--;` that does not mean it is good practice. Similarly, it is not a good practice to deserialize a straight array that is not an object. Just because you can does not mean you should.
wisequark
A: 

I have the same problem right now, and the answers does not answer or help in any way. Let's say I have the following object (a Fire Eagle response):

{"start":0, "stat":"ok", "locations":[{"name":"Pensacola, FL", "place_id":"qQ7Vig2bBZsZCy82", "woeid":2470377}], "count":3, "total":3, "query":"address=Pensacola"}

How can I access the "name" value, which is in the "name" object, inside the "locations" object?

I would love to be able to do that with just the TouchJSON framework.

Thank you!

Mathieu
A: 

@Mathieu - I think this is what you are looking for (6 months late, I know :), but I just ran into the same problem)

Copy and pasted from here: http://groups.google.com/group/touchcode-dev/browse_thread/thread/ada885832019f45b

NSArray *tweetsArray = [resultsDictionary objectForKey:@"results"]; 
for (NSDictionary *tweetDictionary in tweetsArray) { 
  NSString *tweetText = [tweetDictionary objectForKey:@"text"]; 
  [tweets addObject:tweetText]; 
} 

To give more context, the JSON that I'm parsing is in the general form
of:

{"results": 
   [ 
     {"text":"tweet1"}, 
     {"text":"tweet2"}, 
     {"text":"tweet3"} 
   ] 
} 
gene tsai
A: 

Not sure if it helps you but check this out http://tempered.mobi/%20

alecnash