简体   繁体   中英

Incorrectly parse json into NSDictionary

I am trying store text fields data into a NSDictionary from json. I have used SBJson for this.

 {  
   "fields":[  
      {  
        "textFields":[  
            {  
              "text":"Congratulations",
              "textSize":"12"
            },
            {  
               "text":"Best Wishes",
               "textSize":"15"
             },
            {  
              "text":"Test  text",
              "textSize":"10"
            }
          ]
       },
      {  
         "imageFields":[  
            {  
               "image":"test1.jpg",
               "width":"200",
           "height":"100"
        },
        {  
           "image":"test2.jpg",
           "width":"200",
           "height":"100"
            }
         ]
       }
    ]
  }

My code:

 -(void)readJson{

     NSDictionary *jsonDict = [jsonString JSONValue];
     NSDictionary *fieldsDict =[jsonDict valueForKey:@"fields"];
     NSDictionary *textFieldsDict = [fieldsDict valueForKey:@"textFields"];
     NSLog(@" Dictionary %@ ",textFieldsDict );

}

But its output as follows.

Dictionary (
     (
             {
         text = Congratulations;
         textSize = 12;
     },
             {
        text = "Best Wishes";
        textSize = 15;
    },
            {
         text = "Test  text";
         textSize = 10;
     }
 ),
 "<null>"
) 

It seems like there are two items in dictionary and one is null. I wanted to put three textfield items into the array. How can i solve this.

  1. Don't use SBJSON. Use NSJSONSerialization.
  2. Don't use valueForKey: , use objectForKey: .
  3. You are mixing up dictionaries and arrays. Don't do that. Use NSArray for arrays.

I am revising your code for better understanding

 -(void)readJson
 {
     NSDictionary *jsonDict = [jsonString JSONValue];
     NSDictionary *fieldsDict =[jsonDict valueForKey:@"fields"];
     NSDictionary *textFieldsDict = [fieldsDict valueForKey:@"textFields"];
     NSLog(@" Dictionary %@ ",textFieldsDict );
 }

More appropriate way is

-(void)readJson
{
     NSDictionary *jsonDict = [jsonString JSONValue];
     NSArray *fieldsArr =[jsonDict objectForKey:@"fields"];
     for(int i=0;i<[fieldArr count];i++)
     {
         NSArray *textFieldArr = [fieldArr objectAtIndex: i];
         for(int j=0;j<[textFieldArr count];j++)
         {
              NSDictionary *dicTextField = [textFieldArr objectAtIndex: j];
              NSString *text = [dicTextField objectForKey: @"text"];
              NSString *textSize = [dicTextField objectForKey: @"textSize"];
         }         
     }     
}

For quick help treat { as dictionary and [ as array.

Hope, i am helpful to you.

As your json format, [jsonDict valueForKey:@"fields"] will return an array not dictionary so your code must be

NSDictionary *jsonDict = [jsonString JSONValue];
NSArray *fields = [jsonDict objectForKey:@"fields"];
NSDictionary *fieldsDict = fields[0];
NSArray *textFieldsDict = [fieldsDict objectForKey:@"textFields"];
I have corrected the json format and used NSJSONSerialization, 

  {"fields":
   {"textFields":
    [  {"text":"Congratulations", "textSize":"12"},
       {"text":"Best Wishes", "textSize":"15"},
       {"text":"Test  text", "textSize":"10"}
    ],
  "imageFields":
   [  {"image":"test1.jpg","width":"200", "height":"100"},
      {"image":"test2.jpg", "width":"200", "height":"100"}
   ]
 }
}


 -(void)readJson
    NSError *e = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
    NSDictionary *fields = [jsonDict objectForKey:@"fields"];
    NSArray *textArray=[fields objectForKey:@"textFields"] ;
    NSLog(@"--- %@",textArray );
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM