gpt4 book ai didi

iphone - 无法将json字符串解析为nsdictionary

转载 作者:行者123 更新时间:2023-12-01 17:10:23 26 4
gpt4 key购买 nike

我正在尝试使用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

最佳答案

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

-(void)parseJsonString
{

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

NSDictionary *jsonDict = [jsonArray objectAtIndex:0];

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

}

关于iphone - 无法将json字符串解析为nsdictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7036172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com