user1248747
user1248747

Reputation: 31

SBJSON parser doesn't work on second try

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

Upvotes: 1

Views: 655

Answers (2)

Paul Hunter
Paul Hunter

Reputation: 4463

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
}

Upvotes: 1

iOS Developer
iOS Developer

Reputation: 1723

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.

Upvotes: 0

Related Questions