【问题标题】:Parsing into Dictionary解析成字典
【发布时间】:2014-11-13 15:00:43
【问题描述】:

我无法解析以下 JSON 响应。这是我的代码

{
"result": "Success",
"data": "[
 {
    "name": "heal.jpg",
    "date": "2014-11-06 05:06:42"
},
{
    "name": "google.png",
    "date": "2014-11-06 05:09:17"
},
{
    "name": "d.jpg",
    "date": "2014-11-10 06:57:16"
}
]",
"msg": ""
}

代码

 NSString *myJSON = [[NSString alloc] initWithData:resData encoding:NSUTF8StringEncoding];
     NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
     NSDictionary *onlineReadingdata = [jsonData valueForKeyPath:@"data"];
      NSLog(@"Array Disctionary %@",onlineReadingdata);
   for (NSDictionary *details in onlineReadingdata) 
      {
         NSLog(@"name %@",[details valueForKey:@"name"]);
         NSLog(@"date %@",[details valueForKey:@"date"]);
     }

【问题讨论】:

  • 你的JSON不是缺少{/}的开头和结尾吗?
  • 我错过了添加问题,我现在添加了
  • 这是JSONObjectWithData:,而不是JSONObjectWithString:。为什么要将(可能)非常好的 NSData 对象转换为 NSString 并尝试解析它??
  • 正如 Seryozha 指出的那样,您向我们展示的 JSON 列表是虚假的。我很难相信如果您打算这样做,您甚至可以安排生成这样的列表,因为“数组”中的引号没有被转义。我猜你是零零碎碎地粘贴在一起的。

标签: objective-c json parsing dictionary


【解决方案1】:

JSON 无效。 JSON 数组写在方括号内。
删除方括号前后的双引号 (")。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多