Reputation: 13785
I am new in iphone , I have try lot but did not found any solutions.
I want output like this
[
{
"longitude" : -71.449162,
"latitude" : 42.734752
},
{
"longitude" : -71.447343,
"latitude" : 42.735636
},
{
"longitude" : -71.446246,
"latitude" : 42.735807
}
]
But I get output like this
"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",
)
I want longitude and latitude value in separate array.
Thanks
Upvotes: 0
Views: 96
Reputation: 6481
Please try this:
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);
Upvotes: 2
Reputation: 2243
Store latitude and longitude as string format in seperate array and try using this code
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);
Hope this Helps...
Upvotes: 0