My current code I use without the json is:
parts = [NSArray arrayWithObjects:
[[part alloc] initWithpartName:@"Part1" imageName:@"picture1.jpg"],
[part alloc] initWithpartName:@"Part2" imageName:@"picture2.jpg"],
[part alloc] initWithpartName:@"Part3" imageName:@"picture3.jpg"]
nil];
The json I fetch is:
[{"id":"1","case_name":"NZXT Phantom
410","case_description":"","case_type":"ATX","case_color":"White","case_price":"99.99","case
_image":""},{"id":"2","case_name":"Thermaltake
MK+","case_description":"","case_type":"ATX","case_color":"Black","case_price":"84.99","case
_image":""}
I use this to fetch it:
NSString *urlString = [NSString stringWithFormat:@"http://site.com/get/parts/part.php?part=case"];
NSURL *url = [NSURL URLWithString:urlString];
NSString *data = [NSData dataWithContentsOfURL:url];
Question: How could I put this json into this array? I have SBJson and I tried using this:
SBJsonParser *parser = [[SBJsonParser alloc] init];
parts = [[parser objectWithString:data error:nil] copy];
Can someone correct me as to how I parse it with the extra data like color , description, type
When you use SBJSON or any other JSON parser to parse this JSON string, what you will get back is an NSArray where each item of the array is an NSDictionary. The key/value pairs you are looking for will be within those dictionaries. It will NOT directly give you an array of “part” objects – You will have to write code to do that manually.
If that is what you are trying to do, then the code would look something like this:
If you have more fields in your Part class that you want to populate using data from the JSON, then you can add code that sets those fields based on the result of
dict objectForKey:with the appropriate key name, such asidorcase_description.