首页 文章

除了我已经使用的JSON之外,还有更好的方法来解析这个JSON吗?

提问于
浏览
0

我最近有办法从Google Maps API获取我的邮政编码数据 . 在这个问题的最后,我提供了一个从Google收到的JSON响应字符串的大片段 .

在我的代码深处,我得到了响应,并使用Jonathan Wight的TouchJSON库解析它 . 这是我的代码

NSStringEncoding encoding;

// Fetch data from web service.
NSString *jsonString = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:nil];
// Process JSON data returned from web service.
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];

if (jsonString == nil)
{
// bah
} else if (![self isCancelled]) {// Check to see if we have been cancelled.

    if (dictionary) {
        NSArray* results = [dictionary objectForKey:@"results"];
        NSDictionary* types = [results objectAtIndex:1];
        NSArray* address_components = [types objectForKey:@"address_components"];
        NSDictionary* postcodes = [address_components objectAtIndex:0];
        NSString* postcode = [postcodes objectForKey:@"long_name"];
    // do stuff with the postcode
    }
//
}

请注意我如何通过使用NSArray中的特定偏移量来获取类型字典和邮政编码字典 . 我的问题是这个,

How can I parse the JSON string (preferably using TouchJSON) without having to hard code those array offsets?

我觉得谷歌 Map 的响应字符串可能会发生变化,我的硬编码方法会在此时停止工作 . 有没有人使用类似但更强大的方法,他们愿意在这里分享?

...Google Maps API sample response string... The 'target' postcode is highlighted in bold

{“status”:“OK”,“results”:[{“types”:[“street_address”],“formatted_address”:“9 Clarendon Pl,Leamington Spa,Warwickshire CV32 5,UK”,“address_components”:[ {“long_name”:“9”,“short_name”:“9”,“types”:[“street_number”]},{“long_name”:“Clarendon Pl”,“short_name”:“A452”,“types”: [“route”]},{“long_name”:“Leamington Spa”,“short_name”:“Leamington Spa”,“types”:[“locality”,“political”]},{“long_name”:“Warwick”, “short_name”:“Warwick”,“types”:[“administrative_area_level_3”,“political”]},{“long_name”:“Warwickshire”,“short_name”:“Warks”,“types”:[“administrative_area_level_2”,“政治“]},{”long_name“:”英格兰“,”short_name“:”英格兰“,”类型“:[”administrative_area_level_1“,”political“]},{”long_name“:”英国“,”short_name“ :“GB”,“types”:[“country”,“political”]},{“long_name”:“CV32 5”,“short_name”:“CV32 5”,“types”:[“postal_code_prefix”,“postal_code “]}],”geometry“:{”location“:{”lat“:52.2919849,”l ng“: - -1.5399505},”location_type“:”RANGE_INTERPOLATED“,”viewport“:{”southwest“:{”lat“:52.2888374,”lng“: - 1.531216},”northeast“:{”lat“:52.2951326, “lng”: - 1.5368264}},“bounds”:{“southwest”:{“lat”:52.2918320,“lng”: - 1.5399760},“northeast”:{“lat”:52.2921380,“lng”: - 1.5399720 },}“{types”:[“postal_code”],“formatted_address”:“Leamington Spa,Warwickshire CV32 5LD,UK”,“address_components”:[{“long_name”:“CV32 5LD”,“short_name”: “CV32 5LD”,“类型”:[“postal_code”]},{“long_name”:“Leamington Spa”,“short_name”:“Leamington Spa”,“类型”:[“locality”,“political”]}, .........更多JSON如下

1 回答

  • 2

    我从未使用过TouchJSON,但在我看来,您的硬编码偏移是因为您只期望一个结果,但Google Maps API足够灵活,可以返回多个结果 . 实际上,如果你有 objectAtIndex:1 表示你正在使用第二个结果集,那么你编写的代码实际上取决于至少有两个结果 .

    即使我在那里弄错了细节,我怀疑你真正想做的事情而不是 NSDictionary* types = [results objectAtIndex:1]; 更像是:

    NSDictionary* types = [results objectAtIndex:[results indexOfObjectPassingTest:yourPredicateHere]];
    

    如果你不想首先阅读Apple的谓词编程指南,以弄清楚如何设置你的谓词(我在上面标记为 yourPredicateHere 的东西) . http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/predicates.html

相关问题