gpt4 book ai didi

iOS:geojson iOS 访问

转载 作者:行者123 更新时间:2023-11-28 22:35:20 25 4
gpt4 key购买 nike

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

这里是JsonDecoder.m

@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:

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

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

谢谢!

最佳答案

在 iOS >= 5 中你可以在没有外部库的情况下解析它

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 字符串。

如果可能,将其更改为:

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

然后您可以通过以下方式访问它:

NSDictionary *shelf1 = [json objectForKey:@"shelf1"];

//OR

NSArray *coordinates = [[json objectForKey:@"shelf1"] objectForKey:@"coordinates"];

关于iOS:geojson iOS 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16221735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com