简体   繁体   中英

cannot parse json string into nsdictionary

Im trying to parse a json string using sbjsonparser. Im having trouble converting it to nsdictionary. I've used sbjsonparser in my other classes and they all worked fine. see my code.

-(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];

}

here's my console 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)

I checked the string at http://jsonformatter.curiousconcept.com/ and it appears to be valid. what do you think is causing this problem? thanks!

I printed the error in dict = [parser objectWithString:jsonString error:nil];and it says:

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

EDIT I tried hardcoding the jsonstring like this

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];

and I got what I want in the console:

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

EDIT In case you want to know where I get the data. I downloading a zip file from a website using asihttprequest and the this file is extracted using objective-zip and the extracted file is read like this.

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

Your json is an array of one object, so you can't directly parse it to NSDictionary. First parse it to NSArray and then take first object and put it into a NSDictionary

-(void)parseJsonString
{

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

   NSDictionary *jsonDict = [jsonArray objectAtIndex:0];

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

}

objectWithString:error: is having return type id , modify your code as below and let me know.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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