How to get values from this JSON String in Objective C, iPhone -


i wanted parse json string looks this.

there more code in json string, pasting sample here.

[  {  "mainhd":"select correct adjective given options.",     "sub":      [         {             "quetn":"bill 2 years ___ wanda.",             "answr":"1",               "optns":"1,2",                                    "1":"younger",                 "2":"smaller"         },         {             "quetn":"france ___ european country.",             "answr":"2",             "optns":"1,2",                                    "1":"an",                 "2":"a"         }] } ] 

for parsing above json, doing in objective c code.

nsdata *jsondata = [jsonstring datausingencoding:nsutf8stringencoding];     nserror* error;      nsdictionary* json = [nsjsonserialization                           jsonobjectwithdata: jsondata //1                           options:kniloptions                           error:&error];      //nslog(@"json : %@",json);     if(error){         nslog(@"error %@", [error localizeddescription]);     }      nsstring *latestloans = [json valueforkey:@"mainhd"];     nslog(@"mainhd: %@", latestloans); //3      nsstring *sub = [json valueforkey:@"sub"];     nslog(@"sub: %@", sub); //3 

while executing code, getting output.

sub: (         (  {             1 = younger;             2 = smaller;             answr = 1;             optns = "1,2";             quetn = "bill 2 years ___ wanda.";         },                 {             1 = an;             2 = a;             answr = 2;             optns = "1,2";             quetn = "france ___ european country.";         },                 {             1 = gorgeous;             2 = gorgeousest;             3 = gorgeouser;             answr = 1;             optns = "1,2,3";             quetn = "arya looking _______ in dress.";         } 

) )

after want values "sub".

i trying this, not working

 nsdata *mydata = [nskeyedarchiver archiveddatawithrootobject:sub];      //nsdata *jsondata2 = [sub datausingencoding:nsutf8stringencoding];     nsarray *arr = [nsjsonserialization jsonobjectwithdata:mydata                                                    options:0 error:&error];       nslog(@"arr: %@", arr); //3 

i getting null.

it's worth noting in json posted, value sub array, need treat such , loop through values

    [  {  "mainhd":"select correct adjective given options.",     "sub":      [         {             "quetn":"bill 2 years ___ wanda.",             "answr":"1",               "optns":"1,2",                                    "1":"younger",                 "2":"smaller"         },         {             "quetn":"france ___ european country.",             "answr":"2",             "optns":"1,2",                                    "1":"an",                 "2":"a"         }] } ] 

so following should point in right direction

          nsarray *subarray = [json valueforkey:@"sub"];          //loop array using each response dictionary          (nsdictionary *key in subarray){  //log value console          nslog (@"value %@", [key objectforkey:@"quetn");      } 

hope helps


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -