开发者

Parsing JSON value in objective c error?

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=O开发者_开发百科bject 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


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜