sbjsonparser를 사용하여 json 문자열을 구문 분석하려고합니다. 임 nsdictionary로 변환하는 데 문제가 있습니다. 나는 다른 클래스에서 sbjsonparser를 사용했고 모두 잘 작동했습니다. 내 코드를 참조하십시오.json string을 nsdictionary로 구문 분석 할 수 없습니다.
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/에서 문자열을 확인하고 유효한 것으로 나타납니다
-(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];
}
여기에 내 콘솔 출력입니다. 이 문제의 원인은 무엇이라고 생각하십니까? 감사!
dict = [파서 objectWithString : jsonString 오류 : nil];에서 오류를 인쇄했습니다. 그리고는 말한다 :
Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Illegal start of token []"
UserInfo=0x62eb920 {NSLocalizedDescription=Illegal start of token []}
편집 나는이
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];
처럼 jsonstring을 하드 코딩 시도하고 내가 콘솔에서 원하는 걸 얻었 :
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
에 넣어하지만 그 배열 – Robin
임하지 내가 바로 당신을 확실히 이해 사전이 아니라 임 이 코드를 사용하여 json 문자열을이 코드를 사용하여 nsdictionary로 변환합니다. 이 코드 조각은 다른 모든 클래스에서 잘 작동합니다. 이 피드는 저에게 오류를줍니다. SBJsonParser * parser = [[SBJsonParser alloc] init]; NSDictionary * dict; dict = [파서 objectWithString : jsonString 오류 : nil]; NSLog (@ "% @", dict); – Diffy
문자열이 배열이면 강제로 사전으로 변환 할 수 없습니다. [JSON 표준] (http://www.json.org/) 봐 - JSON에서 "개체"NSDictionary 있지만 "배열"NSArray/ –