我试图从openweather.com解析JSON。我将city.list.json.gz下载到NSData。将此文件解压缩到内存中,然后解压缩到NSData,但结果无效。 我将此JSON解压缩为NSString,并得到结果:
{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}
{"_id":1270260,"name":"State of Haryāna","country":"IN","coord":{"lon":76,"lat":29}}
但是我不能解析这行到NSDictionary或NSArray。解析时出错:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x7ff9c0e317b0 {NSDebugDescription=Garbage at end.}
代码:
NSError *err = nil;
NSString *jsonCitiesString = [[NSString alloc] initWithData:[self uncompressGZip:data] encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonCitiesString);
NSArray *cityDictionary = [NSJSONSerialization JSONObjectWithData:[jsonCitiesString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingAllowFragments error:&err];
NSLog(@"%@", err);