当前位置:编程学习 > wap >>

小弟愚钝,求大神帮看看这个json用sbjson怎么解析

functionString = [{\"TYPE\":\"打印机\",
  \"CONFIGDATA\":
                            [ {
                              \"DCONF_ID\":\"207\",
                              \"D_FUN\":\"通电\",
                              \"D_TYPE\":\"zigbee\",
                              \"D_VA\":\"d401\",
                              \"D_STATE\":\"可用\"
                             },
                             {\"DCONF_ID\":\"208\",
                              \"D_FUN\":\"断电\",
                              \"D_TYPE\":\"zigbee\",
                              \"D_VAL\":\"d400\",
                              \"D_STATE\":\"可用\"}]
                            },
  {\"TYPE\": \"电饭煲\",
  \"CONFIGDATA\":\"NULL\"
  }]




主要是下面这个null;
SBJsonParser *parser = [[SBJsonParser alloc]init];
 NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[parser objectWithString:functionString]];
for(NSMutableDictionary *singleHomAppFunctions in array){
   NSMutableArray *arr = [singleHomAppFunctions objectForKey:@"CONFIGDATA"];  //就是这句话我直接用集合接收的话碰到下面CONFIGDATA的“NULL”,就会出错。。。。。。。。大神呀,这咋办呀????、求指导呀!!!!
} --------------------编程问答-------------------- 这是数组格式。很常见的。
我用的是JSONDecoder类来解析的。很简单的。解析方法是这样的

NSDictionary *json = [subjson objectForKey:@"functionString"];
NSArray *responseObject = [json objectForKey:@"CONFIGDATA"];
这样就放到数组里了。如果还不明白,请联系我,我给你写一个demo演示。 --------------------编程问答-------------------- functionString = [{\"TYPE\":\"打印机\",
  \"CONFIGDATA\":
你是不是最外全少了"" --------------------编程问答--------------------  NSString *jsonStr = @"{\"data\":{\"count\":\"14\",\"list\":[{\"groupName\":\"我的好友\",\"friendList\":[{\"name\":\"Phone1\",\"imagename\":\"head01\"},{\"name\":\"Phone2\",\"imagename\":\"head02\"}]},{\"groupName\":\"同事\",\"friendList\":[{\"name\":\"Phone3\",\"imagename\":\"head03\"},{\"name\":\"Phone4\",\"imagename\":\"head04\"},{\"name\":\"Phone4S\",\"imagename\":\"head05\"}]},{\"groupName\":\"我的XX\",\"friendList\":[]}]},\"mesage\":\"操作成功\",\"status\":\"0\"}";
这样才可以解析 --------------------编程问答--------------------  NSString *json = @" {\"data\":{\"list\":[{\"TYPE\":\"打印机\",\"CONFIGDATA\":[ {\"DCONF_ID\":\"207\",\"D_FUN\":\"通电\",\"D_TYPE\":\"zigbee\",\"D_VA\":\"d401\",\"D_STATE\":\"可用\"},{\"DCONF_ID\":\"208\",\"D_FUN\":\"断电\",\"D_TYPE\":\"zigbee\",\"D_VAL\":\"d400\",\"D_STATE\":\"可用\"}]},{\"TYPE\": \"电饭煲\",\"CONFIGDATA\":[]}]}}";
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *dataDci = [dic objectForKey:@"data"];
    NSArray *dataArr = [dataDci objectForKey:@"list"];
    for (NSDictionary *tmpDic in dataArr) {
        NSString *type = [tmpDic objectForKey:@"TYPE"];
        NSLog(@"tyoe %@",type);
        NSArray *arr = [tmpDic objectForKey:@"CONFIGDATA"];
        for (NSDictionary *dic in arr) {
            NSString *D_STATE = [dic objectForKey:@"D_STATE"];
            NSLog(@"D_STATE%@",D_STATE);
        }
    }
补充:移动开发 ,  iPhone
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,