0

可能重复:
解析 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]

如何解析此类数据。

检查这个链接

根据服务提供商,它是 UTF-8 JSON 数据类型。如果不是,请告诉我它是哪种类型的数据以及如何解析它。

请帮帮我。

谢谢。

4

1 回答 1

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"];

你现在有一个NSArraywith 5 valid JSON NSStrings。您可以处理其中任何一个,也可以NS通过如下枚举将它们全部转换为 -Class 对象:

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.

于 2012-09-09T02:15:26.753 回答