【问题标题】:Incorrectly parse json into NSDictionary错误地将 json 解析为 NSDictionary
【发布时间】:2015-02-06 21:45:39
【问题描述】:

我正在尝试将文本字段数据从 json 存储到 NSDictionary。我为此使用了 SBJson。

 {  
   "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{

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

}

但它的输出如下。

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

字典中似乎有两项,一项为空。我想将三个文本字段项放入数组中。我该如何解决这个问题。

【问题讨论】:

  • SBJson 有效率问题。尝试使用iOS的NSJSONSerialization

标签: ios json nsdictionary sbjson


【解决方案1】:
  1. 不要使用 SBJSON。使用 NSJSONSerialization。
  2. 不要使用valueForKey:,使用objectForKey:
  3. 您正在混淆字典和数组。不要那样做。将 NSArray 用于数组。

【讨论】:

  • NSArray *textFieldsDict = [fieldsDict objectForKey:@"textFields"];resulting [__NSArrayM objectForKey:]: 无法识别的选择器错误
  • 那是因为fieldsDict 是一个数组,而不是字典。
【解决方案2】:

我正在修改你的代码以便更好地理解

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

比较合适的方式是

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

寻求快速帮助 将{ 视为字典,将[ 视为数组。

希望对你有帮助。

【讨论】:

    【解决方案3】:

    作为您的 json 格式,[jsonDict valueForKey:@"fields"] 将返回一个数组而不是字典,因此您的代码必须是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多