0

这是我的字典:

 { @"RoutePolyline":<__NSArrayM 0x283983750>(<c59272f7 39263940      55a4c2d8 42f65240>),
    @"RideID":6565
};

我在我的 API 调用中将此字典作为参数发送。

我的应用程序在这行代码崩溃:

 NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];

这是它抛出的错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入中的类型无效 (NSConcreteValue)”

我知道 RoutePolyline 参数包含一个 NSValue (它应该是一个坐标数组)而不是任何对象类型,但我尝试了很多转换,但到目前为止没有任何效果。例如

[NSValue valueWithMKCoordinate:*routeCoordinates]
4

2 回答 2

2

循环遍历您的NSValue数组并提取CLLocationCoordinate2D的值。

for (NSValue *value in coordinates) {
    CLLocationCoordinate2D coordinate;
    [value getValue:&coordinate];
    NSDictionary *coDic = @{@"latitude" : [NSNumber numberWithDouble: coordinate.latitude],
                            @"longitude": [NSNumber numberWithDouble: coordinate.longitude]};
    [array addObject:coDic];
}

还要在序列化之前检查字典是否是有效的 JSON

if ([NSJSONSerialization isValidJSONObject:dic]) {
    NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
}
于 2019-06-19T06:47:13.310 回答
0

首先获取坐标(经纬度)值并存储到数组中,然后可以序列化,它不应该崩溃。尝试使用NSStringapi 中的值:

NSArray *arr = @[ @{@“lat”: [NSString stringWithFormat:@"%ld",routeCoordinates.latitude],
                    @“long”:[NSString stringWithFormat:@"%ld",routeCoordinates.longitude]
                }];
于 2019-06-19T07:32:15.247 回答