Friday, January 10, 2014

Brother dull, seeking God to help look great with sbjson how to resolve this json

functionString = [{\ "TYPE \": \ "Printer \",
\ "CONFIGDATA \":
[{
\ "DCONF_ID \": \ "207 \",
\ "D_FUN \": \ "powered \",
\ "D_TYPE \": \ "zigbee \",
\ "D_VA \": \ "d401 \",
\ "D_STATE \": \ "available \"
},
{\ "DCONF_ID \": \ "208 \",
\ "D_FUN \": \ "off \",
\ "D_TYPE \": \ "zigbee \",
\ "D_VAL \": \ "d400 \",
\ "D_STATE \": \ "available \"}]
},
{\ "TYPE \": \ "cooker \",
\ "CONFIGDATA \": \ "NULL \"
}]




Mainly following null;
SBJsonParser * parser = [[SBJsonParser alloc] init];
NSMutableArray * array = [[NSMutableArray alloc] initWithArray: [parser objectWithString: functionString]];
for (NSMutableDictionary * singleHomAppFunctions in array) {
NSMutableArray * arr = [singleHomAppFunctions objectForKey: @ "CONFIGDATA"]; / / is this sentence I received directly by the collection, then get the following CONFIGDATA of "NULL", an error occurs. . . . . . . . Great God Yeah, this supposed to die? ? ? ? , Seeking to guide! ! ! !
}
Reply:
This is an array format. Very common.
I use JSONDecoder class to parse. Very simple. Such analytical methods are

NSDictionary * json = [subjson objectForKey: @ "functionString"];
NSArray * responseObject = [json objectForKey: @ "CONFIGDATA"];
This will put the array. If you do not understand, please contact me, I write you a demo presentation.
Reply:
functionString = [{\ "TYPE \": \ "Printer \",
\ "CONFIGDATA \":
You are not the outermost whole less ""
Reply:
NSString * jsonStr = @ "{\" data \ ": {\" count \ ": \" 14 \ ", \" list \ ": [{\" groupName \ ": \" My friends \ ", \" friendList \ ": [{\" name \ ": \" Phone1 \ ", \" imagename \ ": \" head01 \ "}, {\" name \ ": \" Phone2 \ ", \" imagename \ ": \ "head02 \"}]}, {\ "groupName \": \ "colleague \", \ "friendList \": [{\ "name \": \ "Phone3 \", \ "imagename \": \ " head03 \ "}, {\" name \ ": \" Phone4 \ ", \" imagename \ ": \" head04 \ "}, {\" name \ ": \" Phone4S \ ", \" imagename \ ": \ "head05 \"}]}, {\ "groupName \": \ "My XX \", \ "friendList \": []}]}, \ "mesage \": \ "Operation successful \", \ "status \": \ "0 \"} ";
So that it can be resolved
Reply:
NSString * json = @ "{\" data \ ": {\" list \ ": [{\" TYPE \ ": \" Printer \ ", \" CONFIGDATA \ ": [{\" DCONF_ID \ ": \" 207 \ ", \" D_FUN \ ": \" powered \ ", \" D_TYPE \ ": \" zigbee \ ", \" D_VA \ ": \" d401 \ ", \" D_STATE \ ": \" available \ "}, {\" DCONF_ID \ ": \" 208 \ ", \" D_FUN \ ": \" power \ ", \" D_TYPE \ ": \" zigbee \ ", \" D_VAL \ ": \" d400 \ ", \" D_STATE \ ": \" available \ "}]}, {\" TYPE \ ": \" cooker \ ", \" 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);
}
}

Reply:
With this test it and see what json data format is
http://json.parser.online.fr/

No comments:

Post a Comment