[英]Parsing JSON value in objective c error?
我使用 ASIHTTPRequest 獲取令牌,並收到以下字符串:
{Token:[{"new":"jkajshdkjashdjhasjkdhjkhd+sd==sfbjhdskfbks+sdjfbs=="}]}
我從這里使用了 JSON 框架: http://stig.github.com/json-framework 。
這是我得到字符串后的代碼:
- (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);
我收到此錯誤:
-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object key string expected\" UserInfo=0x5a418a0 {NSLocalizedDescription=Object key string expected}"
)
我的猜測是,我使用的 JSON 框架無法理解響應字符串格式:
{Token:[{"new":"jkajshdkjashdjhasjkdhjkhd+sd==sfbjhdskfbks+sdjfbs=="}]}
有沒有其他方法來解析值?
快樂的編程,喬尼
它正在尋找圍繞着 Token 的起點和終點。
{"Token":[{"new":"jkajshdkjashdjhasjkdhjkhd+sd==sfbjhdskfbks+sdjfbs=="}]}
這段代碼有一個錯誤:
SBJsonParser *jsParser = [SBJsonParser new];
id content = [responseString JSONValue];
if(!content){
// Parsing error
NSLog(@"%@", jsParser.errorTrace);
}
您實例化一個解析器jsParser
,但不使用它來解析字符串。 ( JSONValue
方法在內部實例化了一個新的解析器。)然后,您從解析器中讀取了未用於解析字符串的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.