如何在iphone中实现获取JSON对象的代码?

时间:2012-07-09 06:05:00

标签: iphone json ios5

大家好我是一名新的iphone开发者。我收到了我所需的URL的响应,如下所示,我需要获取"CategoryID":1的名称和"CategoryID":2的名称都在单独的数组中。如何在iphone中实现获取JSON对象的代码?谢谢 获得回复的代码:

theXML = [[NSString alloc] initWithBytes: [RoutData mutableBytes] length:[RoutData length] encoding:NSUTF8StringEncoding];
NSLog(@"---->>>>>>-->>>:%@",theXML);

响应:

LocationTracking[1807:f803] ---->>>>>>-->>>:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getGearLockerCategoriesResponse xmlns="http://tempuri.org/"><getGearLockerCategoriesResult><status>SUCCESS</status><errorType /><errorMessage /><jsonString>[{"CategoryID":1,"ItemCount":1,"level":1,"Name":"Boots","Description":"Boots descrition","childs":[{"CategoryID":2,"ItemCount":1,"level":2,"Name":"Cold Water","Description":"Cold Water Description","childs":[]}]}]</jsonString><serverTimestamp>63477507673796</serverTimestamp></getGearLockerCategoriesResult></getGearLockerCategoriesResponse></soap:Body></soap:Envelope>

<jsonString>

[{"CategoryID":1,"ItemCount":1,"level":1,"Name":"Boots","Description":"Boots 

descrition","childs":[{"CategoryID":2,"ItemCount":1,"level":2,"Name":"Cold 

Water","Description":"Cold Water Description","childs":[]}]}]

</jsonString>

<serverTimestamp>63477390375625</serverTimestamp>

</DiveTravelerResponse> 

2 个答案:

答案 0 :(得分:0)

Touch JSON库是一个很好的解决方案:https://github.com/TouchCode/TouchJSON

答案 1 :(得分:0)

NSJSONSerialization。如果您的JSON位于名为'str'的NSString中:

NSError *jsonError = nil;
NSArray *rootObjectArray = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
if (rootObjectArray)
{
   /* you have a foundation object */
}
else
{
   /* something went wrong */
   NSLog(@"jsonError: %@", [jsonError localizedDescription]);
}