【问题标题】:Parsing multiple row from json info in objective-c从objective-c中的json信息解析多行
【发布时间】:2012-05-12 21:01:05
【问题描述】:

我的json搅拌是:

{

"locations" :[
       {                
        id = 0;
        lat = "41.653048";
        long = "-0.880677";
        name = "LIMPIA";
       },
       {
        id = 1;
        lat = "41.653048";
        long = "-0.890677";
        name = "LIMPIA2";
       }
  ]

}

使用:

NSDictionary * root = [datos_string1 JSONValue];
NSArray *bares = (NSArray *) [root objectForKey:@"locations"];  

   for (NSArray * row in bares) {
    NSString *barName1 = [bares valueForKey:@"name"];
    NSLog(@"%@",barName1);
    }

我从 NSlog 获得,两次 otput ( 林匹亚, LIMPIA2 )

所以有些不对劲。我需要为每个项目提取 di 单值参数(lat、lon 和 nombre)(以便在 mapkit 应用程序中使用)。你可以帮帮我吗?

【问题讨论】:

  • 这不是有效的 JSON,看起来更像是一个旧的 NeXTSTEP 格式的 PLIST...
  • 发送!对不起,我错了。我只是编辑 JSON。我在解析时遇到了问题。

标签: objective-c json parsing


【解决方案1】:

我需要为每个项目提取两个单值参数(lat、lon 和 nombre)(以便在 mapkit 应用程序中使用)

如果我正确理解了您的问题,您正在尝试访问 locations 数组中每个字典中的每个值,对吗?

为了访问每个值(如果这确实是您的问题),这应该有效:

NSDictionary *root = [datos_string1 JSONValue];
NSArray *bares = (NSArray *)[root objectForKey:@"locations"];  

// Each item in the array is a dictionary, not an NSArray
for (NSDictionary *dict in bares) {
    // Loop over keys
    for (NSString *key in [dict allKeys]) {
        NSLog(@"dict[%@] == %@", key, [dict objectForKey:key]);
    }
}

【讨论】:

  • 谢谢!您的方法允许按顺序访问每个值。很有用。
猜你喜欢
  • 1970-01-01
  • 2013-06-06
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 2013-05-23
  • 1970-01-01
相关资源
最近更新 更多