0

我写了一个简单的 json 来表示相同位置的位置,例如:

{
        lat = "41.653048";
        long = "-0.880677";
        name = LIMPIA;
    },

在 iphone 编程中,我的应用程序能够在地图上显示位置。我的问题是如何在多个位置复制相同的。例如使用像这样的json:

    {
    lat = "41.653048";
    long = "-0.890677";
    name = name1 ;
},

    {
    lat = "41.653048";
    long = "-0.890677";
    name = name2;
},

我怎么能知道项目的数量?我必须更改 json 在表示中添加任何其他内容?或者我必须完成更改它?

4

3 回答 3

1

您需要使用 JSON 数组:

[
  {
    lat = "41.653048";
    long = "-0.890677";
    name = name1 ;
  },
  {
    lat = "41.653048";
    long = "-0.890677";
    name = name2;
  },
]
于 2012-05-11T19:26:27.950 回答
0

我不确定我是否理解您的问题,但您似乎想知道如何最好地在一个 JSON 对象中包含多个点?

{ 
  "locations": 
   [ 
      {
        lat = "41.653048";
        long = "-0.890677";
        name = name1 ;

      }, 
      {
        lat = "41.653048";
        long = "-0.890677";
        name = name2;

      }
   ] 
}
于 2012-05-11T19:27:27.043 回答
0

不确定您是否要从/向对象解析或写入 JSON。我概述了两者。

@interface MyPointModel : NSObject

@property (nonatomic, strong) NSNumber *latitude;
@property (nonatomic, strong) NSNumber *longitude;
@property (nonatomic, strong) NSString *name;

@end

@implementation MyPointModel

@synthesize latitude, longitude, name;

@end

解析

(以及您在哪里进行解析)

@implementation ParsingController

...
- (NSArray *)buildArrayOfMyPointModelWithString:(NSString *)json {

    NSArray *array = [jsonParser.objectWithString:json error:NULL];
    NSMutableArray *arrayOfMyPointModel = [[NSMutableArray alloc] init];

    for (id jsonElement in array) {
        MyPointModel *m = [[MyPointModel alloc] init];

        m.latitude = [jsonElement valueForKey:@"lat"];
        m.longitude = [jsonElement valueForKey:@"long"];
        m.name = [jsonElement valueForKey:@"name"];

        [arrayOfMyPointModel addObject:m];
    }

    return (NSArray *)arrayOfMyPointModel;
}

写作

(我的观点的样本数组)

NSMutableArray *a = [[NSMutableArray alloc] init];
MyPointModel *m1 = [[MyPointModel alloc] init];
MyPointModel *m2 = [[MyPointModel alloc] init];
m1.latitude = [NSNumber numberWithDouble:0.1];
m1.longitude = [NSNumber numberWithDouble:0.01];
m1.name = @"M1";
m2.latitude = [NSNumber numberWithDouble:0.2];
m2.longitude = [NSNumber numberWithDouble:0.02];
m2.name = @"M2";
[a addObject:m1];
[a addObject:m2];

- (NSString *)buildJsonStringWithMyPointModelArray:(NSArray *)array {

    SBJsonWriter *writer = [[SBJsonWriter alloc] init];

    return [writer stringWithObject:array];
}
于 2012-05-11T19:27:27.770 回答