gpt4 book ai didi

iphone - 在 iOS 中将 NSArray 字典转换为 JSON 数组

转载 作者:行者123 更新时间:2023-12-03 16:02:16 24 4
gpt4 key购买 nike

我需要以 JSON 数组格式将 NSArray 发送到服务器。如何将其转换为 JSON。这是我必须传递的 NSArray 示例。

array([0] => array('latitude'=>'10.010490', 
'longitude'=>'76.360779',
'altitude'=>'30.833334',
'timestamp'=>'11:17:23',
'speed'=>'0.00',
'distance'=>'0.00');

[1] => array('latitude'=>'10.010688',
'longitude'=>'76.361378',
'altitude'=>'28.546305',
'timestamp'=>'11:19:26',
'speed'=>'1.614',
'distance'=>'198.525711')
)`

所需的格式是这样的

[
{ "latitude":"10.010490",
"longitude":"76.360779",
"altitude":"30.833334",
"timestamp":"11:17:23",
"speed":"0.00",
"distance":"0.00"
},
{
"latitude":"10.010688",
"longitude":"76.361378",
"altitude":"28.546305",
"timestamp":"11:19:26",
"speed":"1.614",
"distance":"198.525711"
}
]

有人有解决办法吗?提前致谢。

最佳答案

NSDictionary *firstJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"10.010490", @"latitude",
@"76.360779", @"longitude",
@"30.833334", @"altitude",
@"11:17:23", @"timestamp",
@"0.00", @"speed",
@"0.00", @"distance",
nil];

NSDictionary *secondJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"10.010490", @"latitude",
@"76.360779", @"longitude",
@"30.833334", @"altitude",
@"11:17:23", @"timestamp",
@"0.00", @"speed",
@"0.00", @"distance",
nil];

NSMutableArray * arr = [[NSMutableArray alloc] init];

[arr addObject:firstJsonDictionary];
[arr addObject:secondJsonDictionary];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);

关于iphone - 在 iOS 中将 NSArray 字典转换为 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12299965/

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