views:

239

answers:

3

Hello,

I've added the JSON parser to my project and try to parse a JSON string. On most strings it works as it should, but sometimes it isn't. My first thought was, that the JSON string is not well formed, but I've checked it with several JSON validators and they all say it's correct. I additionally checked the string for some line breaks, but there aren't any in the string.

This is my code:

// My JSON string  
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSError *error = [[NSError alloc] init];

// result is null because of error
NSDictionary *result = [parser objectWithString:json_string error:&error];

When I take a look in the debugger, I get the following in my error variable:

Error Domain=org.brautaset.JSON.ErrorDomain Code=3 UserInfo=0x5168270 "Object value expected for key: items"

This indicates that something isn't right with the items in the string, but as I have said on top, I've checked the JSON string with several validators and all say it's ok.

Does anyone has an idea what can be wrong?

Thank you for helping me.

A: 

It's hard to tell, but from looking the JSON string "items" is the first part where funny characters are used e.g:

"content":"\u003cp\u003e\u003ca href\u003d

Perhaps this is confusing the parser? Or possibly the encoding is wrong?

What do you see when you check the value of json_string in the debugger? E.g:

NSLog(json_string);
pheelicks
A: 

Yes, maybe this is the problem. The characters like \u003c are unicode escapes and are coming from the orinal stream, so the encoding should be fine. Is there any way of transforming them into json compatible format?

Dominik
A: 

I checked the TouchJSON parser at http://code.google.com/p/touchcode/wiki/TouchJSONHowTo and this one is able to parse the string correctly.

Thank you all for helping me. :-)

Dominik