从 IOS 中的 2 个数组创建 JSON
create JSON from 2 arrays in IOS
我正在尝试创建字典以 POST JSON 数据到服务器:
NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lon", nil];
NSArray *values = [NSArray arrayWithObjects: orderClass.extraLat, orderClass.extraLon, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
这给了我:
{
lat = (
"54.720746",
"54.719206",
"54.717466"
);
lon = (
"56.011108",
"56.008510",
"56.007031"
);
}
但目标是 POST 格式数组中的数据:
[{"lat":"54.720746", "lon":"56.011108" },
{ "lat":"54.719206", "lon":"56.008510"},
{ "lat":"54.717466", "lon":"56.007031"}]
需要你的帮助。
感谢关注!
正如我在评论中所说的那样——您需要倒转步骤来实现您的目标。首先是字典,然后是数组。你会得到你想要的。
NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lon", nil];
NSArray *lats = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSArray *lons = [NSArray arrayWithObjects:@"4", @"5", @"6", nil];
// your way (not what you want)
NSArray *values = [NSArray arrayWithObjects: lats, lons, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
// my recommendation based on what you want
NSMutableArray *postData = [[NSMutableArray alloc] init];
for (int i = 0; i < lats.count; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[lats objectAtIndex:i] forKey:@"lat"];
[dict setObject:[lons objectAtIndex:i] forKey:@"lon"];
[postData addObject:dict];
}
这将为您带来这些结果:
(lldb) po postDict
{
lat = (
1,
2,
3
);
lon = (
4,
5,
6
);
}
和
(lldb) po postData
<__NSArrayM 0x7ffb5be4d950>(
{
lat = 1;
lon = 4;
},
{
lat = 2;
lon = 5;
},
{
lat = 3;
lon = 6;
}
)
我正在尝试创建字典以 POST JSON 数据到服务器:
NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lon", nil];
NSArray *values = [NSArray arrayWithObjects: orderClass.extraLat, orderClass.extraLon, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
这给了我:
{
lat = (
"54.720746",
"54.719206",
"54.717466"
);
lon = (
"56.011108",
"56.008510",
"56.007031"
);
}
但目标是 POST 格式数组中的数据:
[{"lat":"54.720746", "lon":"56.011108" },
{ "lat":"54.719206", "lon":"56.008510"},
{ "lat":"54.717466", "lon":"56.007031"}]
需要你的帮助。 感谢关注!
正如我在评论中所说的那样——您需要倒转步骤来实现您的目标。首先是字典,然后是数组。你会得到你想要的。
NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lon", nil];
NSArray *lats = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSArray *lons = [NSArray arrayWithObjects:@"4", @"5", @"6", nil];
// your way (not what you want)
NSArray *values = [NSArray arrayWithObjects: lats, lons, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
// my recommendation based on what you want
NSMutableArray *postData = [[NSMutableArray alloc] init];
for (int i = 0; i < lats.count; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[lats objectAtIndex:i] forKey:@"lat"];
[dict setObject:[lons objectAtIndex:i] forKey:@"lon"];
[postData addObject:dict];
}
这将为您带来这些结果:
(lldb) po postDict
{
lat = (
1,
2,
3
);
lon = (
4,
5,
6
);
}
和
(lldb) po postData
<__NSArrayM 0x7ffb5be4d950>(
{
lat = 1;
lon = 4;
},
{
lat = 2;
lon = 5;
},
{
lat = 3;
lon = 6;
}
)