我从我的网络服务器返回了以下JSON对象。我无法弄清楚如何解析它......我基本上想要为每个驱动程序提取uname和profilePic(在这个例子中有两个驱动程序)。我对其他东西不感兴趣。
这是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
}
}
这就是我试图编写的代码(无济于事......):
//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"]];
}
}
我希望有人可以提供帮助。
解决方案:
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"]);
}
答案 0 :(得分:1)
使用SBJSON https://github.com/stig/json-framework
SBJSON *jsonReader = [[[SBJSON alloc] init] autorelease];
NSDictionary *responce = [jsonReader objectWithString:[request responseString]];
NSString *uName1 = [[responce objectForKey:@"0"] objectForKey:@"uname"];
NSString *image1 = [[responce objectForKey:@"0"] objectForKey:@"profilePic"];