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.

有帮助吗?

解决方案

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

:)

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top