我正在尝试使用JSON来解析php文件中的某些输出,这些输出有望从我的MySQL数据库中返回一些数据。但是,我收到错误:-JSONValue失败。错误是:非法启动令牌[A]。 php脚本返回一个字典数组,用于在地图上绘制注释。我做错了什么?
- (void)getDeviceLocations {
NSLog(@"Getting Device Locations");
NSString *hostStr = @"http://98.246.50.81/firecom/api/getdevicelocations.php";
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
id object = [serverOutput JSONValue];
NSMutableArray *array = (NSMutableArray *)object;
for (NSDictionary *dictionary in array) {
CLLocationCoordinate2D coord = {[[dictionary objectForKey:@"latitude"] doubleValue], [[dictionary objectForKey:@"longitude"] doubleValue]};
Annotation *ann = [[Annotation alloc] init];
ann.title = [dictionary objectForKey:@"deviceid"];
ann.coordinate = coord;
[mapView addAnnotation:ann];
}
}
答案 0 :(得分:0)
正如我评论的那样,您的JSON无效。您应该返回这样的有效JSON(使用您的数据)
[
{
"latitude": 45.5113,
"deviceid": "E19 iPad",
"longitude": "-122.800233"
},
{
"latitude": 45.5458,
"deviceid": "E05 iPad",
"longitude": "-122.9588931798935"
},
{
"latitude": 45.5207,
"deviceid": "E01 iPad",
"longitude": "-122.98941537737846"
}
]
我认为最好尝试使用default php JSON encode method json_encode生成json字符串。