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

如何在iphone中实现获取JSON对象的代码?,iphone,json,ios5,Iphone,Json,Ios5,大家好,我是一个新的iphone开发者。我从如下所示的所需URL获得响应。我需要获得“CategoryID”:1的名称和“CategoryID”:2的名称都在单独的数组集中。如何在iphone中实现获取JSON对象的代码?。非常感谢。 获取响应的代码: theXML = [[NSString alloc] initWithBytes: [RoutData mutableBytes] length:[RoutData length] encoding:NSUTF8StringEncoding];

大家好,我是一个新的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> 
LocationTracking[1807:f803]--->>>>>-->:SUCCESS[{“CategoryID”:1,“ItemCount”:1,“level”:1,“Name”:“Boots”,“Description”:“Boots Description”,“childs”:[{“CategoryID”:2,“ItemCount”:1,“level”:2,“Name”:“Cold Water”,“Description”:“Cold Water Description”,“childs”:[]}]63477507673796
[{“CategoryID”:1,“ItemCount”:1,“level”:1,“Name”:“Boots”,“Description”:“Boots”
描述,“childs”:[{“CategoryID”:2,“ItemCount”:1,“level”:2,“Name”:“Cold”
水“,”描述“:”冷水描述“,”儿童“:[]}]]
63477390375625

Touch JSON库是一个很好的解决方案:

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]);
}

获取NSMutableDictionary中的所有数据,然后设置条件。然后根据条件添加NSMutableArray,然后从数组中获取值。如果要绕过NSJSONSerialization,我建议使用
JSONKit
。嗨,朋友,我已经使用了您给定的Ans代码来满足我的请求,但问题是当我实现的代码类似于NSError*jsonError=nil;NSArray*rootObjectArray=[NSJSONSerialization JSONObjectWithData:[theXML dataUsingEncoding:NSUTF8StringEncoding]选项:0错误:&jsonError];但是我得到的回应是2012-07-10 07:59:35.020 LocationTracking[1807:f803]--******--:(null)如何解决这个问题