[英]how to Json request convert to native object or list?
{
"id": "1",
"result": [
{
"Name": "John",
"Statu": "Online"
},
{
"Name": "Alex",
"Statu": "Online"
},
{
"Name": "Diaz",
"Statu": "Offline"
}
]
}
How do i extract each "car" JSON object and put it into a native object? 如何提取每个“汽车” JSON对象并将其放入本机对象? I tried several way but i can't do that. 我尝试了几种方法,但我做不到。
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"]);
}
But ıt gıves an error tooç I want to just lıst them but ı cant do thatç can anybody help me please? 但是,我也想给他们一个错误,但我不能这样做,请问有人可以帮助我吗? thank you for reading 谢谢你的阅读
Use an Array to capture the responseString: 使用数组来捕获responseString:
NSString *responseString = [request responseString];
NSArray *array = [responseString JSONValue];
Then when you need an individual item from that array use a Dictionary: 然后,当您需要该数组中的单个项目时,请使用字典:
// 0 is the index of the array you need
NSDictionary *itemDictionary = (NSDictionary *)[array objectAtIndex:0];
Given a JSON responseString that looks like this: 给定一个如下所示的JSON responseString:
[{"UniqueID":111111,"DeviceName":"DeviceName1","Location":"Device1Loc","Description":"Device1Desc"},{"UniqueID":22222,"DeviceName":"DeviceName2","Location":"Device2Loc","Description":"Device2Desc"}] [{“ UniqueID”:111111,“ DeviceName”:“ DeviceName1”,“ Location”:“ Device1Loc”,“ Description”:“ Device1Desc”},{“ UniqueID”:22222,“ DeviceName”:“ DeviceName2”,“位置“:” Device2Loc“,”描述“:” Device2Desc“}]
You will wind up with an Array that looks like this: 您将得到一个看起来像这样的数组:
myArray = (
{
Description = "Device1Desc";
DeviceName = "DeviceName1";
Location = "Device1Loc";
UniqueID = 111111;
},
{
Description = "Device2Desc";
DeviceName = "DeviceName2";
Location = "Device2Loc";
UniqueID = 222222;
}
)
And a Dictionary of index 0 that looks like this: 索引为0的字典如下所示:
myDictionary = {
Description = "Device1Desc";
DeviceName = "DeviceName1";
Location = "Device1Loc";
UniqueID = 111111;
}
Sorry for any confusion and improperly instantiated object earlier. 对于任何混乱和较早实例化的对象,我们深表歉意。 I am still a relative newbie that learned something today. 我还是一个相对的新手,今天学到了一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.