views:

30

answers:

2

So i'm using touch JSON which worked out pretty well for me. I was able to take an array, put it in a dictionary, serialize it via touchJSON and send it out via http.

now on the return end, i received the data, and put it into a dictionary (i'm using trends.json from twitter as an example JSON).

now if i try to get the value for trends from the dictionary object, i get this http://cl.ly/fe270fe7f05a0ea8d478

If I try to get the value for name or URL, I get nothing which is frustrating. That is the data I will need. You can tell it is in a dictionary format because it is formatted and it is reading properly sort of for trends. I'm pretty sure I'm missing something, so please let me know which direction to follow.


Here is the code:

// this is all touch JSON magic. responseString has the full contents of trends.json

 NSString *response = [request responseString];
 NSLog(@"response value is:%@",response);

 NSString *jsonString = response;
 NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
 NSError *error = nil;
 NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
//end of touchJSON.  It is in a dictionary now.


 NSLog(@"dictionary:%@, error %@", dictionary, error); //http://cl.ly/adb6c6a974c3e70fb51c

 NSString *twitterTrends = (NSString *) [dictionary objectForKey:@"trends"];
 NSLog(@"ANSWER:%@",twitterTrends); //http://cl.ly/fe270fe7f05a0ea8d478
A: 

You've only got as far as extracting the array of dictionaries. (The response has a dictionary inside an array inside a dictionary.)

You need to extract each dictionary from the array, then lookup the name and url entries in that dictionary.

Something like this should print the first entry:

NSArray *twitterTrends = [dictionary objectForKey:@"trends"];
NSDictionary *entry1 = [twitterTrends objectAtIndex:0];
NSLog(@"entry1: %@, %@", [entry1 objectForKey:@"name"], [entry1 objectForKey:@"url"]);

(Code has not been tested so may not compile straight off!)

JosephH
thanks this helps.
Robbie A.
A: 

When you use print an object using a formatstring %@ you will infact get the output of the description message send to the object. For a NSDictionary this output looks very similar to unparsed JSON, but it is not a string, it's an object graph of NSDictionaries, NSArrays, NSStrings, NSNumbers and NSDates.

So want you have in twitterTrends an NSArray of NSDictionaries. To get the parts just enumerate the Array.

for (NSDictionary* trend in twitterTrends) {
    NSString* url = [trend objectForKey:@"url"];
    NSString* name = [trend objectForKey:@"name"];
}

Or you may access a trend by its index:

[[twitterTrends objectAtIndex:5] objectForKey:@"url"];
tonklon
Thanks, this is what I was looking for. I took a level up and extracted the trends dictionary in that code. Here is the final codefor (NSDictionary *item in [dictionary objectForKey:@"trends"]){NSString *name = [item objectForKey:@"name"];NSString *url = [item objectForKey:@"url"];
Robbie A.