0

I'm stuck at json object parsing really tried hard. The problem is how to parse the json object. Here's what i get the response in the log.

{"0":{"**title**":"Test Event","url_title":"test_event1","status":"open","entry_date":"Sep 10, 2012, 

05:20:38AM","entry_id":"26","site_id":"1","channel_id":"3","field_dt_40":null,"field_dt_58":null,"channel_title":"News & 

Events","channel_name":"news_events","start_date":"1348120800","end_date":"1348120800","start_time": "43200","end_time":"46800","where":"FCF","news_event_description":"<p>\n\tLunch with group.<\/p>\n"},

"1":{"**title**":"Test Event 2","url_title":"test_event_2","status":"open","entry_date":"Sep 10, 2012, 05:20:08AM","entry_id":"28","site_id":"1","channel_id":"3","field_dt_40":null,"field_dt_58":null,"channel_title":"News & Events","channel_name":"news_events","start_date":"1348207200","end_date":"1348207200","start_time":"43200","end_time":"46800","where":"FCF - Lunch","news_event_description":"<p>\n\tThis was a great event.<\/p>\n"},

"2":{"**title**":"Test Event 3","url_title":"test_event_3","status":"open","entry_date":"Sep 10, 2012, 05:20:54AM","entry_id":"29","site_id":"1","channel_id":"3","field_dt_40":null,"field_dt_58":null,"channel_title":"News & Events","channel_name":"news_events","start_date":"1346738400","end_date":"1346738400","start_time":"7200","end_time":"11700","where":"FCF - Lunch","news_event_description":"<p>\n\tFall planning season.<\/p>\n"}}

The problem is i want to show all the titles in the tableview. I can get the single Title by using key 0,1,2. But i want all the titles to be shown at once i parse

Please help me out guys, Thanks in advance.

Neo
  • 2,807
  • 1
  • 16
  • 18
Joker
  • 734
  • 3
  • 11
  • 27
  • http://stackoverflow.com/questions/3165290/how-to-parsing-json-object-in-iphone-sdk-xcode-using-json-framework this link may help you.. – SachinVsSachin Oct 08 '12 at 05:30

2 Answers2

3

Suppose jsonDict is your json dictionary.... Try this

NSArray * keys=[[NSArray alloc]init];
keys=[jsonDict allKeys];
NSMutableArray *titles=[[NSMutableArray alloc]init];
for(int i=0;i<[keys count];i++){
      [titles addObject:[[jsonDict valueForKey:[keys objectAtIndex:i]]valueForKey:@"title"]];
}
NSLog(@"your array of titles : %@",titles); //use this array to fill your cell
Neo
  • 2,807
  • 1
  • 16
  • 18
  • Love u bro.. Seriously man ur genious.. Thank u so much,.. God bless u bro... I'll keep in touch man....dont forget me :) – Joker Oct 08 '12 at 05:35
  • 3
    @Neo, your first line is unnecessary -- you create an empty array and then assign the same variable to the array returned by allKeys. It should just be NSArray *keys = [jsonDict allKeys]; – rdelmar Oct 08 '12 at 05:45
2

Are you trying to parse the JSON yourself? You might find it easier to use something that's already well tested, such as TouchJSON or Apple's own NSJSONSerilization. The result should be a graph of Objective-C objects that you can use however you like.

In any case, what you've got there is the equivalent of a dictionary of dictionaries. If you have that as a NSDictionary called myJSONDictionary, you can say:

NSArray *theObjects = [myJSONDictionary allValues]; // gets all the objects
NSArray *theTitles = [theObjects valueForKey:@"**title**"]; // gets all the titles

You can also iterate through a dictionary using fast enumeration:

NSMutableArray *theTitles = [NSMutableArray array];
for (NSString *key in myJSONDictionary) {
    NSDictionary *object = [myJSONDictionary objectForKey:key];
    NSString *title = [object objectForKey:@"**title**"];
    [theTitles addObject:title]
}

There's no real advantage to doing that instead of using KVC as in the first example if you just need the titles, but it could be the right choice if you have more complex work to do for each object.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • ok thanks. But, help me out bro. I just need some code that will parse the json object dynamically. In my question 0,1,2 are there, that may increase r decrease, So i want to parse all the keys dynamically. Plz plz – Joker Oct 08 '12 at 05:25