简体   繁体   English

解析目标 c 中的 JSON 值错误?

[英]Parsing JSON value in objective c error?

I use ASIHTTPRequest to get a token, and I receive this string:我使用 ASIHTTPRequest 获取令牌,并收到以下字符串:

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

I used JSON Framework from here: http://stig.github.com/json-framework .我从这里使用了 JSON 框架: 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:我的猜测是,我使用的 JSON 框架无法理解响应字符串格式:

{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 的起点和终点。

{"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.您实例化一个解析器jsParser ,但不使用它来解析字符串。 (The JSONValue method instantiates a new parser internally.) Then you read the error from the parser that was not used to parse the string . JSONValue方法在内部实例化了一个新的解析器。)然后,您从解析器中读取了未用于解析字符串的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM