URL:http://www.12beli.com/App/Index/Category?pid=0 往服务器发数据返回的是数组
服务器返回数据如下:
dict-123--{
CategoryName = Komputer;
ID = 4;
ParentID = 0;
child = "[{\"ID\":16,\"CategoryName\":\"Laptop\",\"ParentID\":4},{\"ID\":15,\"CategoryName\":\"Desktop\",\"ParentID\":4},{\"ID\":14,\"CategoryName\":\"Printer\",\"ParentID\":4},{\"ID\":24,\"CategoryName\":\"Aksesori Komputer\",\"ParentID\":4},{\"ID\":21,\"CategoryName\":\"Proyektor\",\"ParentID\":4},{\"ID\":204,\"CategoryName\":\"Networking\",\"ParentID\":4},{\"ID\":209,\"CategoryName\":\"Lain-Lain\",\"ParentID\":4},{\"ID\":215,\"CategoryName\":\"Hardware\",\"ParentID\":4}]";
}-
如要取child里面的内容需要将json字符串转换,否则按正常取法的话会crash
参考:
NSMutableString *jsonDataString=[NSMutableString string];
[jsonDataString setString:[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"jsonData" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]];
NSData *nsData=[jsonDataString dataUsingEncoding:NSUTF8StringEncoding];
parsedArray=[NSJSONSerialization JSONObjectWithData:nsData options:kNilOptions error:nil];
我解析的内容 (ASIHTTPRequest)
- (void)requestFinished:(ASIHTTPRequest *)request
{
SBJSON * json = [[SBJSONalloc]init];
_dataArray = [jsonobjectWithString:[request responseString]error:nil];
if ([_dataArraycount] != 0) {
for (NSDictionary * dictin _dataArray) {
NSString *str= [dict objectForKey:@"child"];
NSData *nsData=[strdataUsingEncoding:NSUTF8StringEncoding];
NSArray *childArray=[NSJSONSerializationJSONObjectWithData:nsData options:kNilOptionserror:nil];
if ( childArray != nil ) {
// for(int i=0;i<[childArray count];i++)
// {
// [childArray objectAtIndex:i];
// }
for (NSDictionary * littleDictin childArray) {
NSString * name = [littleDict objectForKey:@"CategoryName"];
}
}
}
}
}