1

When I call the SBJsonParser the first time, it's working fine but on the second try, the result is always null. The data format is exactly the same as the first.

Here is the code:

  -(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;
  }

Here are the print out for NSLog lines above:

First try:

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

Second Try:

  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)

As you can see, I am doing the init every time. Not sure why it's not working. Any suggestion is appreciated.

Thank you

user1248747
  • 31
  • 1
  • 4

2 Answers2

1

In your second try the JSON is not valid. That is why it is not being parsed. You can check it here: http://jsonlint.com/

At the end of the JSON string it seems like some junk has been inserted. If you format the string you will find the problem at lines 51-53. Replace the following with a comma:

]
 }{
 "postalCodes":

On closer inspection it looks like you are only interested in placeName == "Elizabeth". At the very end you have one entry where placeName == "Fanwood". So you probably just want to remove lines 51-62.

As an aside, you could use the error parameter to detect problems with your parser.

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

if (error) {
    // we have a problem
}
Paul Hunter
  • 4,453
  • 3
  • 25
  • 31
  • I updated the second try code with error message print out added in. I tried to remove some lines before manually for posting - I might have remove too many lines. The output now is as is. I am calling the same web service to get the result for first and second try so, the format should be correct. – user1248747 May 29 '12 at 00:23
  • The JSON is still completely malformed. I don't know what your data source is, but it is not feeding you working JSON. – Paul Hunter May 29 '12 at 00:53
  • Try to copy-paste the JSON into the box in this page: http://jsonlint.com/ . You will see that it is not valid. – Paul Hunter May 29 '12 at 00:56
  • I see the issue. The first try result for jsonString got appended to the second try thus, the invalid string. Thank you for your help!! – user1248747 May 29 '12 at 01:06
0

Why an unexpected array braces closing here?in your second json sring?

"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"}]} .

Its not an issue of init.its an error in the jsonstring that you are getting.

iOS Developer
  • 1,723
  • 2
  • 16
  • 47