I have the following JSON object being returned from my webserver. I can’t figure out how to parse it… I basically want to extract the uname and the profilePic for each of the drivers (there are two drivers in this example). I’m not interested in the rest of the stuff.
Here is the JSON:
{
"0": {
"uname": "Eamorr",
"fname": "Bill",
"lname": "Byrne",
"phoneNumber": "087-2342404",
"roofSignNumber": "32984",
"reputation": 0,
"vehicleMake": "Toyota",
"vehicleModel": "Carina",
"vehicleNumPassengers": "4",
"profilePic": "XE7F654O05RC3I33P44A",
"online": 1,
"lat": "52.27114461",
"lng": "-9.70294471",
"status": "done!",
"sex": "M",
"picList": ["W725CY1XR63PW480Z694", "XBA2078W4Z3JROQMSN16", "0H0XDD27J9J9RV28KKR4", "84G09NCP537G1KM6O4R8", "FM12F2J12AWL8C8XX2F5", "5AU4FLJ50PN0R210AP8J", "5588ORG95RF10B757NY7", "4z54cz5c565410r2wq2u", "3502IXTI31MSX6Z01NWC", "XE7F654O05RC3I33P44A", "A828K8M0E5576C1AK6HU", "M5EHX8MQMHZ6PJ1NNYH7", "8RIS3245542E2I9TLOD4", "3V5F7HNZNN642O29347Y", "YU2CT34A7769XG2LV38G"],
"last5comments": [{
"comment": "asdf2",
"fromUname": "Anonymous",
"time": 1314036666
}, {
"comment": "qwerty",
"fromUname": "Eamorr",
"time": 1314550970
}, {
"comment": "qwerty",
"fromUname": "Eamorr",
"time": 1314551143
}, {
"comment": "hi",
"fromUname": "Eamorr2",
"time": 1315157494
}, {
"comment": "Hello",
"fromUname": "Anonymous",
"time": 1315394983
}],
"numPagesComments": 13
},
"1": {
"uname": "Eamorr2",
"fname": "Steve",
"lname": "McCloskey",
"phoneNumber": "087-3234404",
"roofSignNumber": "32431",
"reputation": 0,
"vehicleMake": "Toyota",
"vehicleModel": "avensis",
"vehicleNumPassengers": "4",
"profilePic": -1,
"online": "0",
"lat": "52.28783634",
"lng": "-9.66791791",
"status": "",
"sex": "M",
"picList": [],
"last5comments": [{
"comment": "asdf",
"fromUname": "Anonymous",
"time": 1296655686
}, {
"comment": "I'm off in the middle of the Atlantic again",
"fromUname": "Anonymous",
"time": 1296843759
}, {
"comment": "Hi",
"fromUname": "Eamorr",
"time": 1299098148
}, {
"comment": "Hi",
"fromUname": "Eamorr",
"time": 1299098148
}, {
"comment": "Hi",
"fromUname": "Eamorr",
"time": 1299098148
}],
"numPagesComments": 2
}
}
And here’s what I’ve tried to code up (to no avail…):
//NSDictionary *obj=[parser objectWithString:[request responseString] error:nil];
NSArray *obj=[parser objectWithString:[request responseString] error:nil];
NSLog([obj objectAtIndex:0]);
/*for(int i=0;i<obj.count;i++){
NSDictionary *driver=[obj objectAtIndex:i];
for (NSDictionary *dict in driver) {
[unames addObject:[dict objectForKey:@"uname"]];
[profilePics addObject:[dict objectForKey:@"profilePic"]];
}
}
I hope someone can help.
Solution:
SBJsonParser *parser=[[SBJsonParser alloc]init];
//NSDictionary *obj=[parser objectWithString:[request responseString] error:nil];
NSDictionary *obj=[parser objectWithString:[request responseString] error:nil];
for(int i=0;i<[obj count];i++){
NSDictionary *obj2=[obj objectForKey:[NSString stringWithFormat:@"%d",i]];
NSLog(@"%@",[obj2 objectForKey:@"uname"]);
}
Use SBJSON https://github.com/stig/json-framework