无法在 iPhone 的 JSON 框架上访问 NSArray 的内容

发布于 2024-12-17 02:39:16 字数 1428 浏览 3 评论 0原文

我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娇女薄笑 2024-12-24 02:39:16

你的对象是一本字典。可以通过访问返回一个数组

NSArray *aLinkArray = [myJasonArray valueForKey:@"Programs"]

Your object is a dictionary. An array can be returned by accessing

NSArray *aLinkArray = [myJasonArray valueForKey:@"Programs"]
§对你不离不弃 2024-12-24 02:39:16

要访问 link1 值,您应该使用:

[[[myJsonArray objectForKey:@"Programs"] objectAtIndex:0] objectForKey:@"link1"]

To access the link1 value you should use:

[[[myJsonArray objectForKey:@"Programs"] objectAtIndex:0] objectForKey:@"link1"]
榕城若虚 2024-12-24 02:39:16

确保 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 }.

傲鸠 2024-12-24 02:39:16

在您的情况下,解析器返回一个 NSDictionary 而不是 NSArray 。

在访问该对象之前添加 a

NSLog(@"json returns: %@", myJsonArray);

并在控制台中查看结果。

The parser returns a NSDictionary and not an NSArray in your case.

Add a

NSLog(@"json returns: %@", myJsonArray);

before accessing that object and see the results in the console.

感受沵的脚步 2024-12-24 02:39:16

您正在将 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.

凑诗 2024-12-24 02:39:16

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文