无法在 iPhone 的 JSON 框架上访问 NSArray 的内容
我可以使用 JSON-Framework 3.0 成功解析 JSON 文件的内容,但无法提取 NSArray 的所有元素。
应用程序在此行崩溃(如下所示)link = [myJsonArray objectAtIndex:0];
在控制台上给我此消息: -[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x70ea20
。
这是我的代码:
NSError *error;
NSString *link;
NSArray *myJsonArray;
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myJSONfile" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
SBJsonParser *parser = [[SBJsonParser alloc] init];
myJsonArray = [[parser objectWithString:jsonString error:&error] copy];
[parser release];
link = [myJsonArray objectAtIndex:0];
NSLog(@"json returns: %@", myJsonArray);
这是我的 JSON 文件:
{
"Programs": [
{"link1": "http://www.myWebSite1.aspx",
"program name": "Live Show at 9",
"speaker": "Dr. Speaker 1"},
{"link2": "http://www.myWebSite2.aspx",
"name": "Dr. Speaker 2",
"speaker": "Live Show at 10"}
]
}
这是日志输出:
JSON Output: {
Programs = (
{
link1: "http://www.myWebSite1.aspx",
program name: "Live Show at 9",
speaker: "Dr. Speaker 1"},
{link2: "http://www.myWebSite2.aspx",
program name: "Dr. Speaker 2",
speaker: "Live Show at 10"}
);
}
我做错了什么?感谢您的帮助!
I can successfully parse the contents of a JSON file, using the JSON-Framework 3.0, but I'm not being able to extract all elements of the NSArray.
The app crashes at this line (shown below) link = [myJsonArray objectAtIndex:0];
giving me this message on the console: -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x70ea20
.
Here is my code:
NSError *error;
NSString *link;
NSArray *myJsonArray;
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myJSONfile" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
SBJsonParser *parser = [[SBJsonParser alloc] init];
myJsonArray = [[parser objectWithString:jsonString error:&error] copy];
[parser release];
link = [myJsonArray objectAtIndex:0];
NSLog(@"json returns: %@", myJsonArray);
And here is my JSON file:
{
"Programs": [
{"link1": "http://www.myWebSite1.aspx",
"program name": "Live Show at 9",
"speaker": "Dr. Speaker 1"},
{"link2": "http://www.myWebSite2.aspx",
"name": "Dr. Speaker 2",
"speaker": "Live Show at 10"}
]
}
This is the Log Output:
JSON Output: {
Programs = (
{
link1: "http://www.myWebSite1.aspx",
program name: "Live Show at 9",
speaker: "Dr. Speaker 1"},
{link2: "http://www.myWebSite2.aspx",
program name: "Dr. Speaker 2",
speaker: "Live Show at 10"}
);
}
What Am I doing wrong? Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的对象是一本字典。可以通过访问返回一个数组
Your object is a dictionary. An array can be returned by accessing
要访问 link1 值,您应该使用:
To access the link1 value you should use:
确保 JSON 文件中的文本以
[
开头并以]
结尾。在这种情况下,objectWithString:
应返回一个数组。现在,它似乎返回一个
NSDictionary
,如果您的文件以{
开头并以}
结尾,就会出现这种情况。Make sure the text in your JSON file starts with
[
and ends with]
. In this case,objectWithString:
should return an array.Right now, it seems, it is returning an
NSDictionary
which would be the case if your file starts with{
and ends with}
.在您的情况下,解析器返回一个 NSDictionary 而不是 NSArray 。
在访问该对象之前添加 a
并在控制台中查看结果。
The parser returns a NSDictionary and not an NSArray in your case.
Add a
before accessing that object and see the results in the console.
您正在将 objectAtIndex: 发送到 NSDictionary,这是错误的。只需尝试打印您分配给 myJsonArray 的内容,然后首先查看它是否是有效的数组。
You are sending objectAtIndex: to a NSDictionary which is wrong. Just try printing what you are assigning to myJsonArray and see if it is a valid array in the first place.
您的 JSON 很可能无效。尝试将 JSON 文本粘贴到 http://jsonlint.com/ 并查看它是否有效。
You most likely have invalid JSON. Try pasting your JSON text into http://jsonlint.com/ and see if it is valid.