Reputation: 15
{
"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? 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
Upvotes: 0
Views: 216
Reputation: 14427
Use an Array to capture the 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:
[{"UniqueID":111111,"DeviceName":"DeviceName1","Location":"Device1Loc","Description":"Device1Desc"},{"UniqueID":22222,"DeviceName":"DeviceName2","Location":"Device2Loc","Description":"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:
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.
Upvotes: 1