簡體   English   中英

無法將 json 字符串解析為 nsdictionary

[英]cannot parse json string into nsdictionary

我正在嘗試使用 sbjsonparser 解析 json 字符串。 我無法將其轉換為 nsdictionary。 我在我的其他課程中使用了 sbjsonparser,它們都運行良好。 看我的代碼。

-(void)parseJsonString
{
    NSLog(@"%@",jsonString);
    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    NSDictionary *dict;
    dict = [parser objectWithString:jsonString error:nil];
    NSLog(@"%@",dict);


    NSDictionary *dict2;
    dict2 = [jsonString JSONValue];
    NSLog(@"%@",dict2);

   [parser release];

}

這是我的控制台 output:

2011-08-12 13:56:55.098 EasyQuiz[5446:13603] [{
"q": "Question Testing", 
"score": 1, 
"c3": "Choice C", 
"c2": "Choice B", 
"c1": "Choice A", 
"rev": 1, 
"id": 1, 
"c4": "Choice D"
}]
2011-08-12 13:56:55.686 EasyQuiz[5446:13603] (null)
2011-08-12 13:56:56.296 EasyQuiz[5446:13603] -JSONValue failed. Error is: Illegal start  
of token []
2011-08-12 13:56:56.297 EasyQuiz[5446:13603] (null)

我在http://jsonformatter.curiousconcept.com/檢查了字符串,它似乎是有效的。 你認為是什么導致了這個問題? 謝謝!

我在 dict = [parser objectWithString:jsonString error:nil]; 中打印了錯誤它說:

  Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Illegal start of token []" 
  UserInfo=0x62eb920 {NSLocalizedDescription=Illegal start of token []}

編輯我嘗試像這樣對jsonstring進行硬編碼

NSString *thisJsonString = @"[{\"q\": \"Question Testing\",\"score\": 1, \"c3\": \"Choice C\", \"c2\": \"Choice B\", \"c1\": \"Choice A\", \"rev\": 1, \"id\": 1, \"c4\": \"Choice D\"}]";

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSDictionary *dict;

dict = [parser objectWithString:thisJsonString error:nil];
NSLog(@"dict %@",dict);

[parser release];

我在控制台中得到了我想要的:

dict (
    {
    c1 = "Choice A";
    c2 = "Choice B";
    c3 = "Choice C";
    c4 = "Choice D";
    id = 1;
    q = "Question Testing";
    rev = 1;
    score = 1;
}
)

編輯如果你想知道我從哪里得到數據。 我使用 asihttprequest 從網站下載了 zip 文件,並且使用 Objective-zip 提取了該文件,並且提取的文件是這樣讀取的。

NSString *filePath = [[self applicationDocumentsDirectory]  
stringByAppendingPathComponent:@"json.zip"];

    //Opening zip file for reading...
    progressLabel.text = @"Reading file...";
    ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];

    //Opening first file...
    progressLabel.text = @"Opening file...";
    [unzipFile goToFirstFileInZip];
    ZipReadStream *read1= [unzipFile readCurrentFileInZip];

    //Reading from first file's stream...
    NSMutableData *data1= [[[NSMutableData alloc] initWithLength:1000000] autorelease];//100MB
    int bytesRead1= [read1 readDataWithBuffer:data1];
    NSLog(@"bytes: %d",bytesRead1);
    if (bytesRead1 > 0) {
        progressLabel.text = @"File is good!";
        jsonString = [[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding] autorelease];
//.... more codes follow, but this is how I get jsonString

您的 json 是一個 object 的數組,因此您不能直接將其解析為 NSDictionary。 首先將其解析為 NSArray 然后先取 object 並將其放入 NSDictionary

-(void)parseJsonString
{

   NSArray *jsonArray = (*NSArray)[jsonString JSONValue];

   NSDictionary *jsonDict = [jsonArray objectAtIndex:0];

   NSString *q = [jsonDict objectForKey:@"q"];
   ...

}

objectWithString:error: 有返回類型id ,修改你的代碼如下,讓我知道。

    NSString *str = [NSString stringWithFormat:@"%@", [parser objectWithString:jsonString error:nil]]; 
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:str,@"response", nil];
    NSLog(@"%@",[dict description]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM