0

我是 iphone 新手,我尝试了很多但没有找到任何解决方案。

我想要这样的输出

 [
        {
           "longitude" : -71.449162,
           "latitude" : 42.734752
        },
        {
           "longitude" : -71.447343,
           "latitude" : 42.735636
        },
        {
           "longitude" : -71.446246,
           "latitude" : 42.735807
        }
     ]

但我得到这样的输出

"longitude":"(
   "42.713974",
   "-71.449432",
   "42.713757",
   "-71.449471",
   "42.713474",
   "-71.449516",

)", "latitude":"(
   "42.713974",
   "-71.449432",
   "42.713757",
   "-71.449471",
   "42.713474",
   "-71.449516",

)

我想要单独的数组中的经度和纬度值。

谢谢

4

2 回答 2

2

请试试这个:

NSMutableArray *mytest = [[NSMutableArray alloc]initWithObjects:@"-71.449162",@"-71.447343",@"-71.446246",nil];

NSMutableArray *mytest1 = [[NSMutableArray alloc]initWithObjects:@"42.734752",@"42.735636",@"42.735807",nil];

NSMutableArray *testArray = [[NSMutableArray alloc]init];
for (int i = 0; i< 3; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[mytest objectAtIndex:i] forKey:@"longitude"];
[dict setObject:[mytest1 objectAtIndex:i] forKey:@"latitude"];
[testArray addObject:dict];


 }

NSLog(@"my array is : %@",testArray);

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:testArray options:NSJSONWritingPrettyPrinted error:&error]; 

NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; 

NSLog(@"json string is : \n%@",jsonString);
于 2013-02-13T10:15:46.903 回答
0

将纬度和经度以字符串格式存储在单独的数组中并尝试使用此代码

NSMutableArray * totalArray=[[NSMutableArray alloc]init];
for(int i=0;i<3;i++)
{
NSArray* temp=[[NSArray alloc]initwithObjects:[NSString stringWithFormat:@"Latitude%@",[latitudearray objectAtIndex:i]],[NSString stringWithFormat:@"Longitudee%@",[longitudearray objectAtIndex:i]],nil];
[totalArray addObject:temp];
 }

  NSLog(@"%@",totalArray);

希望这可以帮助...

于 2013-02-13T07:48:18.400 回答