Note that there are some explanatory texts on larger screens.

plurals
  1. POcannot parse json string into nsdictionary
    primarykey
    data
    text
    <p>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.</p> <pre><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]; </code></pre> <p>}</p> <p>here's my console output:</p> <pre><code>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) </code></pre> <p>I checked the string at <a href="http://jsonformatter.curiousconcept.com/" rel="nofollow">http://jsonformatter.curiousconcept.com/</a> and it appears to be valid. what do you think is causing this problem? thanks!</p> <p>I printed the error in dict = [parser objectWithString:jsonString error:nil]; and it says:</p> <pre><code> Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Illegal start of token []" UserInfo=0x62eb920 {NSLocalizedDescription=Illegal start of token []} </code></pre> <p><strong>EDIT</strong> I tried hardcoding the jsonstring like this</p> <pre><code>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]; </code></pre> <p>and I got what I want in the console:</p> <pre><code>dict ( { c1 = "Choice A"; c2 = "Choice B"; c3 = "Choice C"; c4 = "Choice D"; id = 1; q = "Question Testing"; rev = 1; score = 1; } ) </code></pre> <p><strong>EDIT</strong> 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.</p> <pre><code>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 &gt; 0) { progressLabel.text = @"File is good!"; jsonString = [[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding] autorelease]; //.... more codes follow, but this is how I get jsonString </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload