无法将json字符串解析为nsdictionary

时间:2011-08-12 06:06:01

标签: iphone ios xcode json

我试图使用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];

}

这是我的控制台输出:

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

2 个答案:

答案 0 :(得分:0)

您的json是一个对象的数组,因此您无法直接将其解析为NSDictionary。首先将其解析为NSArray,然后获取第一个对象并将其放入NSDictionary

-(void)parseJsonString
{

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

   NSDictionary *jsonDict = [jsonArray objectAtIndex:0];

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

}

答案 1 :(得分:-1)

objectWithString:错误:返回类型为 id ,请修改您的代码,如下所示。让我知道。

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