【问题标题】:Read data from geoJson file in ios从 ios 中的 geoJson 文件中读取数据
【发布时间】:2015-05-12 06:03:15
【问题描述】:

我正在尝试使用此代码从 geojson 文件中读取数据:

NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"roads_json" ofType:@"geojson"];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[[NSData alloc]
                                                              initWithContentsOfFile:jsonPath]
                                                     options:0
                                                       error:nil];
NSLog(@"points:%@",json);

和输出:

点数:(null)

任何想法请...

【问题讨论】:

  • 你能把geoJSON文件的内容贴出来吗?
  • 您确定文件包含正确的 json 格式吗...您是否在控制台中收到任何错误...
  • @vizllx 是的,包含正确的 json 格式并且控制台中没有错误...
  • 你没有传递错误对象,你怎么知道有没有错误?
  • jsonPath 设置是否正确,您尝试过记录吗?

标签: ios objective-c geojson


【解决方案1】:

在使用NSJSONSerialization 时最好传递NSError 对象。这样你就可以知道到底哪里出了问题。

NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[[NSData alloc]
                                                              initWithContentsOfFile:jsonPath]
                                                     options:0
                                                       error:&error];

if (error)
{
    NSLog(@"Could not serialize GeoJSON file: %@", [error localizedDescription]);
}
else
{
    //GeoJSON has been successfully decoded
}

【讨论】:

  • 无法序列化 GeoJSON 文件:操作无法完成。 (Cocoa 错误 3840。)现在这个错误显示..
  • 基本说明json文件不正确。
  • 我如何设置 json 文件,jsonlint.com :Valid JSON
  • 太好了。您能发布 GeoJSON 文件的内容吗?
  • 至少部分内容可以。
猜你喜欢
  • 2013-06-22
  • 2018-01-24
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多