1

当我第一次调用 SBJsonParser 时,它工作正常,但在第二次尝试时,结果始终为空。数据格式与第一种完全相同。

这是代码:

  -(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)

如您所见,我每次都在进行初始化。不知道为什么它不起作用。任何建议表示赞赏。

谢谢

4

2 回答 2

1

在您的第二次尝试中,JSON 无效。这就是为什么它没有被解析。你可以在这里查看:http: //jsonlint.com/

在 JSON 字符串的末尾,似乎插入了一些垃圾。如果您格式化字符串,您会在第 51-53 行发现问题。用逗号替换以下内容:

]
 }{
 "postalCodes":

仔细观察,您似乎只对placeName== "Elizabeth" 感兴趣。最后,您有一个条目,其中placeName== "Fanwood"。因此,您可能只想删除第 51-62 行

顺便说一句,您可以使用错误参数来检测解析器的问题。

NSError *error = nil;
NSDictionary *results = [parser objectWithString:jsonString error:&error];

if (error) {
    // we have a problem
}
于 2012-05-27T17:52:39.800 回答
0

为什么在您的第二个 json 字符串中出现意外的数组大括号?

"placeName":"Elizabeth","lat":40.653207,"adminName1":"New Jersey"}]}{"postalCodes":{"adminName2":"Union","adminCode2":"039","distance":"3.97758","adminCode1":"NJ","postalCode":"07023","countryCode":"US","lng":-74.386762,"placeName":"Fanwood","lat":40.641856,"adminName1":"New Jersey"}]} .

它不是 init 的问题。它在你得到的 jsonstring 中有一个错误。

于 2012-05-28T05:21:39.297 回答