Incorrectly parse json into NSDictionary

独自空忆成欢 提交于 2019-12-13 03:35:50

问题


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:


  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.



回答2:


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.




回答3:


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"];



回答4:


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 );
 }


来源:https://stackoverflow.com/questions/27371516/incorrectly-parse-json-into-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!