当我第一次调用SBJsonParser时,它工作正常,但是在第二次尝试中,结果始终为null。数据格式与第一种完全相同。
这是代码:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
// the connection finished loading all data, process...
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:self.jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",jsonString);
if ( jsonString != nil ) {
// Create SBJSON object to parse JSON
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *results =
[parser objectWithString:jsonString error:nil];
NSLog(@"results: %@", results);
if ( [[results objectForKey:@"postalCodes"] count] > 0 ) {
// Build an array from the dictionary for easy access to each entry
NSArray *postalCodes_array = [results objectForKey:@"postalCodes"];
int index = 0;
// Loop through each entry in the dictionary...
for (NSDictionary *dict_item in postalCodes_array)
{
if ( index == 0 ) {
appDelegate.curZip = [dict_item objectForKey:@"postalCode"];
NSLog(@"New Zip: %@", appDelegate.curZip);
break;
}
}
postalCodes_array = nil;
parser = nil;
}
results = nil;
}
jsonString = nil;
}
这是上面NSLog行的打印输出:
第一次尝试:
2012-05-27 12:19:13.322[16525:707] {"postalCodes": [{"adminName2":"Union","adminCode2":"039","distance":"0","adminCode1":"NJ","postalCode":"07201","countryCode":"US","lng":-74.2099,"placeName":"Elizabeth","lat":40.661369,"adminName1":"New Jersey"},{"distance":"0.28183","adminCode1":"NJ","postalCode":"07216","countryCode":"US","lng":-74.210939,"placeName":"Elizabeth","lat":40.663778,"adminName1":"New Jersey"},{"distance":"0.28183","adminCode1":"NJ","postalCode":"07215","countryCode":"US","lng":-74.210939,"placeName":"Elizabeth","lat":40.663778,"adminName1":"New Jersey"},{"adminName2":"Union","adminCode2":"039","distance":"1.12041","adminCode1":"NJ","postalCode":"07202","countryCode":"US","lng":-74.221544,"placeName":"Elizabeth","lat":40.65652,"adminName1":"New Jersey"},{"adminName2":"Union","adminCode2":"039","distance":"1.72655","adminCode1":"NJ","postalCode":"07206","countryCode":"US","lng":-74.192487,"placeName":"Elizabeth","lat":40.653207,"adminName1":"New Jersey"}]}
第二次尝试:
2012-05-28 20:16:16.727 [17151:707] {"postalCodes":[{"adminName2":"Kings","adminCode2":"047","distance":"0","adminCode1":"NY","postalCode":"11230","countryCode":"US","lng":-73.956528,"placeName":"Brooklyn","lat":40.618122,"adminName1":"New York"},{"adminName2":"Kings","adminCode2":"047","distance":"1.38292","adminCode1":"NY","postalCode":"11210","countryCode":"US","lng":-73.946682,"placeName":"Brooklyn","lat":40.628064,"adminName1":"New York"},{"adminName2":"Kings","adminCode2":"047","distance":"2.04126","adminCode1":"NY","postalCode":"11229","countryCode":"US","lng":-73.94749,"placeName":"Brooklyn","lat":40.601094,"adminName1":"New York"},{"adminName2":"Kings","adminCode2":"047","distance":"2.45579","adminCode1":"NY","postalCode":"11204","countryCode":"US","lng":-73.985623,"placeName":"Brooklyn","lat":40.617871,"adminName1":"New York"},{"adminName2":"Kings","adminCode2":"047","distance":"2.70498","adminCode1":"NY","postalCode":"11223","countryCode":"US","lng":-73.974291,"placeName":"Brooklyn","lat":40.597874,"adminName1":"New York"}]}{"postalCodes":[{"adminName2":"Richmond","adminCode2":"085","distance":"0","adminCode1":"NY","postalCode":"10306","countryCode":"US","lng":-74.141922,"placeName":"Staten Island","lat":40.564416,"adminName1":"New York"},{"adminName2":"Richmond","adminCode2":"085","distance":"0.41508","adminCode1":"NY","postalCode":"10313","countryCode":"US","lng":-74.146836,"placeName":"Staten Island","lat":40.564393,"adminName1":"New York"},{"adminName2":"Richmond","adminCode2":"085","distance":"1.66907","adminCode1":"NY","postalCode":"10308","countryCode":"US","lng":-74.152649,"placeName":"Staten Island","lat":40.55181,"adminName1":"New York"},{"adminName2":"Richmond","adminCode2":"085","distance":"3.76947","adminCode1":"NY","postalCode":"10312","countryCode":"US","lng":-74.179165,"placeName":"Staten Island","lat":40.545745,"adminName1":"New York"},{"adminName2":"Richmond","adminCode2":"085","distance":"4.41459","adminCode1":"NY","postalCode":"10314","countryCode":"US","lng":-74.147218,"placeName":"Staten Island","lat":40.603915,"adminName1":"New York"}]}
2012-05-28 20:16:16.760 [17151:707] error: Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Token 'start of object' not expected after outer-most array or object" UserInfo=0x176560 {NSLocalizedDescription=Token 'start of object' not expected after outer-most array or object}
2012-05-28 20:16:16.761 [17151:707] results: (null)
如您所见,我每次都在进行初始化。不知道为什么它不起作用。任何建议表示赞赏。
谢谢
最佳答案
在您的第二次尝试中,JSON无效。这就是为什么它没有被解析。您可以在此处检查它:http://jsonlint.com/
在JSON字符串的末尾,似乎已经插入了一些垃圾。如果格式化字符串,您将在第51-53行发现问题。将以下内容替换为逗号:
]
}{
"postalCodes":
经过仔细检查,您似乎只对
placeName
==“ Elizabeth”感兴趣。最后,您只有一个条目,其中placeName
==“ Fanwood”。因此,您可能只想删除第51-62行。顺便说一句,您可以使用error参数来检测解析器的问题。
NSError *error = nil;
NSDictionary *results = [parser objectWithString:jsonString error:&error];
if (error) {
// we have a problem
}