可能重复:
parse UTF-8 JSON ios sdk
[4,"1.0",1347139911696]
[0,"Neathouse Place","58226","STBC",154,"L",51.495322,-0.141808]
[0,"Vauxhall Bridge Rd / Victoria Stn","59516","STBC",160,"Z9",51.495931,-0.142216]
[0,"VICTORIA, VAUXHALL BRIDGE ROAD (EAST)",null,null,0,null,51.495839,-0.142119]
[0,"Vauxhall Bridge Rd / Victoria Stn","54249","STBC",256,"M",51.496573,-0.141354]
如何解析此类数据。
检查此link。
根据服务提供商,它是UTF-8 JSON数据类型。如果不是那么请告诉我它是哪种类型的数据以及如何解析它。
请帮帮我。
感谢。
答案 0 :(得分:1)
首先要意识到的是,您的数据实际上是5个独立但有效的JSON
数组。 JSON
可以jsonlint.com验证// stringWithContentsOfURL: used for demonstration
NSURL *sourceURL = [NSURL URLWithString:@"http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?Circle=51.49598,-0.14091,100&StopPointState=0&ReturnList=StopCode1,StopPointName,Bearing,StopPointIndicator,StopPointType,Latitude,Longitude"];
NSString *actualResponse = [NSString stringWithContentsOfURL:sourceURL encoding:NSUTF8StringEncoding error:nil];
NSArray *individualJSONArrays = [actualResponse componentsSeparatedByString:@"\n"];
。
要从中获取有用的内容,您必须按行拆分响应。您可以使用换行符作为分隔符。
NSArray
您现在有一个JSON
,其中有5个有效NSStrings
NS
。你可以处理它们中的任何一个,或者通过枚举这样将它们全部转换为NSMutableArray *jsonObjects = [[NSMutableArray alloc] init];
for (NSString *jsonString in individualJSONArrays) {
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
if (jsonArray){
[jsonObjects addObject:jsonArray];
}
}
NSLog(@"%@",jsonObjects);
- 类对象:
NSArray
现在,NSArrays
已填充{{1}}。