在为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
]
]
}
}如何从类文件中检索这些值?
谢谢!
发布于 2013-04-26 16:22:15
解决了它。
上面的工作非常完美。
我忘了为geojson文件的项目设置目标成员资格。
为此,标记您的mark文件,单击文件检查器,并在"Target Membership“处切换到项目的复选框。
谢谢。
发布于 2013-04-26 03:31:35
在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"];https://stackoverflow.com/questions/16221735
复制相似问题