首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS: geojson iOS访问

iOS: geojson iOS访问
EN

Stack Overflow用户
提问于 2013-04-26 02:10:17
回答 2查看 2.4K关注 0票数 0

在为iPad进行开发时,我创建了一个名为Coordinates.geojson的文件。我想从一个名为JsonDecoder.m的类文件中访问它

这是JsonDecoder.m

代码语言:javascript
复制
@implementation JsonDecoder



- (id)initWithJson
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Coordinates" ofType:@"geojson"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSError *error;
    _json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    return self;
}

- (NSArray*) getCoordinatesFromShelf:(NSString *) bookShelfName
{
    NSArray *coordinates = [[[_json objectForKey:@"shelf1"] objectForKey:@"coordinates"]objectAtIndex:1];
    for(id i in coordinates)
        NSLog(@"%@",i);
    return coordinates;
}
@end

和我的Coordinates.geojson:

代码语言:javascript
复制
{
    "shelf1": {
        "name": "six.png",
        "coordinates": [
                        [
                         14,
                         25,
                         329,
                         138
                         ],
                        [
                         14,
                         185,
                         329,
                         138
                         ],
                        [
                         14,
                         344,
                         158,
                         138
                         ],
                        [
                         185,
                         344,
                         158,
                         138
                         ],
                        [
                         14,
                         94,
                         158,
                         138
                         ],
                        [
                         185,
                         500,
                         158,
                         138
                         ]
                        ]
    }
}

如何从类文件中检索这些值?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-26 16:22:15

解决了它。

上面的工作非常完美。

我忘了为geojson文件的项目设置目标成员资格。

为此,标记您的mark文件,单击文件检查器,并在"Target Membership“处切换到项目的复选框。

谢谢。

票数 0
EN

Stack Overflow用户

发布于 2013-04-26 03:31:35

在iOS >= 5中,您可以在没有外部库的情况下对其进行解析

代码语言:javascript
复制
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Coordinates" ofType:@"geojson"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

但文件内容不是有效的JSON字符串。

如果可能,将其更改为类似以下内容:

代码语言:javascript
复制
{
    "shelf1": {
        "name": "six.png",
        "coordinates": [
            [
                14,
                25,
                329,
                138
            ],
            [
                14,
                185,
                329,
                138
            ],
            [
                14,
                344,
                158,
                138
            ],
            [
                185,
                344,
                158,
                138
            ],
            [
                14,
                94,
                158,
                138
            ],
            [
                185,
                500,
                158,
                138
            ]
        ]
    }
}

然后,您可以使用以下命令访问它:

代码语言:javascript
复制
NSDictionary *shelf1 = [json objectForKey:@"shelf1"];

//OR

NSArray *coordinates = [[json objectForKey:@"shelf1"] objectForKey:@"coordinates"];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16221735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档