1

I'm sending out a request to an external API and parsing a response with the SBJson parser. However, I suspect the response is so long, it is somehow getting jumbled.

In my mainviewcontroller.h file I set NSMutableData *receivedData; so that I can use it in the connection methods in the mainviewcontroller.m file.

However, after the connection finishes loading, I execute the following:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSArray *allData = [dataString JSONValue];
}  

However, I get a bunch of errors saying that the JSON is not properly formatted. So, when I look at the JSON, its very long – but here and there, there are problems... for example, the "updated_at" below.

  {
            "id": 7844333,
            "position": 3,
            "content": "Cell height is off by 5 pixels",
            "created_at": "2012-06-04T20:31:30-05:00",
            "updated_at": "2ator": {
                "id": 98258,
                "name": "Brian"
            }

What I think happened above is that updated at has a value of "2012-06...etc" and the next key-value item would be creator : { id, name } but it somehow got jumbled into updated at.

Anyone having a similar problem? I don't think the problem is with the JSONValue because I nslog out the dataString before it gets parsed, and thats where I find the JSON errors.

What I mean by that is that NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; is just a long string, but has bad JSON in it because it is jumbled.

Brian Weinreich
  • 7,692
  • 8
  • 35
  • 59
  • What do you see if you just go to the URL in a browser? (Just making 100% sure it's not a problem with the server!) – deanWombourne Jun 05 '12 at 15:04
  • Otherwise, can you add the code where you deal with received data in your NSURLConnection delegate? Can you NSLog each bit of data you receive to check it as it arrives? – deanWombourne Jun 05 '12 at 15:05
  • The browser shows the correct JSON data. It's when I try to build the receivedData by appending one after one, it jumbles it. Ill add that code above. – Brian Weinreich Jun 05 '12 at 16:58
  • 1
    Are you getting two URLs at the same time - if so you might be accidentally using the same `receivedData` for both NSURLConnections? – deanWombourne Jun 06 '12 at 11:10
  • Yeah that ended up being the problem. receivedData was being used all the various connections that I was sending out (simultaneously) and was being appended incorrectly. If you want to submit it as answer below (to gain the rep), I will mark it correct. – Brian Weinreich Jun 06 '12 at 15:36

2 Answers2

2

Are you using receivedData by more than one connection at once?

:)

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
-1

I think your json is wrong. For checking that just put json file into the : http://jsonlint.com/

If it is valid then:

Import the SBJSON framework classes into your project.And try the following code:

 SBJSON *parser=[[SBJSON alloc]init];

NSDictionary * dictionary = [parser objectWithString:responseString];

this will give you data into dictionary then by using:

NSString *firstParseData=[dictionary objectForKey:@"your key"];

you can retrieve the data. Hope this will work in your case.

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • Yeah I know it's wrong. I ran it through JSONLint and it showed an error at the key/value "updated_at" But, what I'm saying is that the data given to the parser is bad. I don't understand why receivedData is building a bad JSON string. It gets corrupted. – Brian Weinreich Jun 05 '12 at 13:31