2

I use ASIHTTPRequest to get a token, and I receive this string:

{Token:[{"new":"jkajshdkjashdjhasjkdhjkhd+sd==sfbjhdskfbks+sdjfbs=="}]}

I used JSON Framework from here: http://stig.github.com/json-framework.

This is the code after I get the string:

- (void)requestFinished:(ASIHTTPRequest *)req
{
    NSString *responseString = [req responseString];
    NSLog(@"Response: %@", [req responseString]);

    // Parse the response
    SBJsonParser *jsParser = [SBJsonParser new];
    id content = [responseString JSONValue];
    if(!content){
        // Parsing error
        NSLog(@"%@", jsParser.errorTrace);
    }
}
NSLog(@"ID: %@", content);

I receive this error:

 -JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object key string expected\" UserInfo=0x5a418a0 {NSLocalizedDescription=Object key string expected}"

)

My guess is that, the JSON Framework that I am using cant understand the response string format:

{Token:[{"new":"jkajshdkjashdjhasjkdhjkhd+sd==sfbjhdskfbks+sdjfbs=="}]}

Is there any other way to parse the value?

Happy programming, Johnie

2 Answers 2

4

It was looking for a start and end " surrounding Token.

{"Token":[{"new":"jkajshdkjashdjhasjkdhjkhd+sd==sfbjhdskfbks+sdjfbs=="}]}
Sign up to request clarification or add additional context in comments.

3 Comments

Oh my.. I didnt notice it. Hmmm, the string is generated by other website, is there way to solve this?
Hi Johnie, you might want to clarify with the website regarding this, some more tolerant JSON parers are more susceptible to mistakes like this, while some are stricter. If you wish to check and rectify every JSON string received pro grammatically it is going to be expensive.
I see... Well, in the end, I solved it in an ugly way. I split the string, add the "(double quotes), and combine the string again, and it works for me now. I will let the website developers know about it. Thanks a lot for your help =)
0

There's a bug in this code:

SBJsonParser *jsParser = [SBJsonParser new];
id content = [responseString JSONValue];
if(!content){
    // Parsing error
    NSLog(@"%@", jsParser.errorTrace);
}

You instantiate a parser, jsParser, but don't use it to parse the string. (The JSONValue method instantiates a new parser internally.) Then you read the error from the parser that was not used to parse the string.

1 Comment

memory leaks.... thank you Stig, i will change the code. Lucky you mentioned it, cuz I keep copy paste this code from my previous projects :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.