{
"id": "1",
"result": [
{
"Name": "John",
"Statu": "Online"
},
{
"Name": "Alex",
"Statu": "Online"
},
{
"Name": "Diaz",
"Statu": "Offline"
}
]
}
如何提取每个“car”JSON对象并将其放入本机对象?我尝试了几种方法,但我做不到。
NSString *responseString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSString *responseDict = [responseString JSONValue];
NSArray *objects = [NSArray arrayWithObjects:[responseDict valueForKeyPath:@"result.Name"],[responseDict valueForKeyPath:@"result.Statu"],nil];
NSLog(@"objects Array: %@",objects);
**==> NSLOG gives:
(
(
"John",
"Alex",
"Diaz"
),
(
"Online",
"Online",
"Offline"
)
)
NSArray *resultsArray = [responseString JSONValue];
for (NSDictionary *personDict in resultsArray)
{
NSLog(@"ihaleAdi =: %@",[carDict valueForKey:@"result.ihaleAdi"]);
NSLog(@"ihaleDurum =: %@",[carDict valueForKey:@"result.Statu"]);
}
但是我也错过了一个错误我只想说明他们但是我不能那样做任何人可以帮助我吗?谢谢你的阅读
答案 0 :(得分:1)
使用数组捕获responseString:
NSString *responseString = [request responseString];
NSArray *array = [responseString JSONValue];
然后,当您需要该数组中的单个项目时,请使用词典:
// 0 is the index of the array you need
NSDictionary *itemDictionary = (NSDictionary *)[array objectAtIndex:0];
给出一个如下所示的JSON responseString:
[{ “的UniqueID”:111111, “设备名称”: “DeviceName1”, “位置”: “Device1Loc”, “描述”: “Device1Desc”},{ “的UniqueID”:22222, “设备名称”: “DeviceName2” , “位置”: “Device2Loc”, “描述”: “Device2Desc”}]
您将看到一个如下所示的数组:
myArray = (
{
Description = "Device1Desc";
DeviceName = "DeviceName1";
Location = "Device1Loc";
UniqueID = 111111;
},
{
Description = "Device2Desc";
DeviceName = "DeviceName2";
Location = "Device2Loc";
UniqueID = 222222;
}
)
索引0的字典看起来像这样:
myDictionary = {
Description = "Device1Desc";
DeviceName = "DeviceName1";
Location = "Device1Loc";
UniqueID = 111111;
}
对于之前的任何混淆和不正确实例化的对象,我们深表歉意。我仍然是今天学到一些东西的相对新手。