Parsing JSON dictionary / array

If the JSON web service returns something like this (home details)

[ { id:4, price: 471, location: "New York", size: 3000 }, { id:7, price: 432, location: "London", size: 3200 }, { id:22, price: 528, location: "Tokyo", size: 2000 } ] 

How can I go through each house one by one? I am using ASIHTTPRequest and the JSON parser: http://stig.github.com/json-framework/

I think I can get the dictionary like this:

 NSString *theResponse = [request responseString]; NSDictionary *dictionary = [theResponse JSONValue]; 

.. but I'm not sure how to go through each house.

+4
source share
1 answer
 { id:4, price: 471, location: "New York", size: 3000 }, { id:7, price: 432, location: "London", size: 3200 }, { id:22, price: 528, location: "Tokyo", size: 2000 } 

This is an array of dictionaries ... you can create the Modal class of your house (attribute: id, price, location, size) and repeat it as follows (given that you finally have the above) ..

 NSArray *houses = [dictionary objectForKey:<youHaveNotProvideItInYourData>]; NSMutableArray *populatedHouseArray = [[NSMutableArray alloc]init]; for(int i=0;i<[houses count];i++) { NSDictionary *tempDictionary = [houses objectAtIndex:i]; House *tempHouse = [[House alloc]init]; if([tempDictionary objectForKey:@"id"]!=nil { tempHouse.id = [tempDictionary objectForKey:@"id"]; } //and so on for other keys [populatedHouseArray addObject:tempHouse]; [tempHouse release]; } 

Thanks,

+9
source

Source: https://habr.com/ru/post/1347949/


All Articles