简体   繁体   English

无法从json访问数据

[英]Cant access data from json

I'm having trouble with JSON. 我在使用JSON时遇到问题。 I made this in PHP and I'm sending it to my JavaScript, but I can't get the values. 我是用PHP制作的,然后将其发送到我的JavaScript,但无法获取值。

[
   {
      "book":[
         {
            "dir":"extract\/pg1065.epub"
         },
         {
            "dir":"extract\/pg1065.epub\/1065\/0.css"
         },
         {
            "dir":"extract\/pg1065.epub\/1065\/1.css"
         },
   }

   {
      "book":[
         {
            "dir":"extract\/pg6130-images.epub"
         },
         {
            "dir":"extract\/pg6130-images.epub\/6130\/0.css"
         },
    }
]

I'm trying to access it with 我正在尝试使用

var obj =  JSON.parse(result);
alert(obj.book[0].dir[1]);

Anyone have any ideas? 有人有想法么?

First you need to validate your json, i have validate your json it gives error. 首先,您需要验证您的json,我已经验证了您的json提供了错误。 In your json dIr is id. 在您的json中, dIr是id。 You have defined 3 dir id for same object this may be error. 您为同一对象定义了3个目录ID,这可能是错误的。

EDIT: I missed it but first comment explains your missing your closing square brackets for the book arrays. 编辑:我错过了,但第一条评论解释了您缺少本书数组的右方括号。 Add that in and your good to go. 加进去,你的好去处。 Validate the JSON first. 首先验证JSON。

You don't need to do JSON.parse you can simply do 您不需要做JSON.parse您可以简单地做

var data = <?php echo "Your generated JSON code"; ?>;

Worth a note you can create your data structure in PHP and then simply use json_encode, then you can be sure it will be valid JSON 值得一提的是,您可以使用PHP创建数据结构,然后仅使用json_encode,那么可以确定它将是有效的JSON。

var data = <?php echo json_encode($yourData); ?>;

You have output an array so to get the first object you will do something like 您已经输出了一个数组,因此要获取第一个对象,您将执行以下操作

var firstObj = data[0];

To get the first dir of the first book 获取第一本书的第一目录

var firstDir = data[0]["book"][0]["dir"];

The code shown in the question is not a valid JSON. 问题中显示的代码不是有效的JSON。 There are missing closing square brackets for each of the book arrays and (thanks to @punund) a missing comma between array members. 每个book数组都缺少右方括号,(感谢@punund)数组成员之间缺少逗号。 The correct JSON would be this: 正确的JSON是这样的:

[
   {
      "book":[
         {
            "dir":"extract\/pg1065.epub"
         },
         {
            "dir":"extract\/pg1065.epub\/1065\/0.css"
         },
         {
            "dir":"extract\/pg1065.epub\/1065\/1.css"
         }
      ]
   },

   {
      "book":[
         {
            "dir":"extract\/pg6130-images.epub"
         },
         {
            "dir":"extract\/pg6130-images.epub\/6130\/0.css"
         }
      ]
   }
]

You should not normally be printing JSON directly, but instead creating a JSON object in PHP and then using json_encode function. 通常,您不应该直接打印JSON,而应该在PHP中创建一个JSON对象,然后使用json_encode函数。 The following PHP will produce valid JSON for your scenario: 以下PHP将为您的情况生成有效的JSON:

<?php

$result = array(
        (object)array("book" => array((object)array("dir" => "extract/pg1065.epub"),
                                      (object)array("dir" => "extract/pg1065.epub/1065/0.css"),
                                      (object)array("dir" => "extract/pg1065.epub/1065/1.css"))),
        (object)array("book" => array((object)array("dir" => "extract/pg6130-images.epub"),
                                      (object)array("dir" => "extract/pg6130-images.epub/6130/0.css")))
);


echo json_encode($result);

?>
[
{
    "book": [
        {
            "dir": "extract/pg6130-images.epub"
        },
        {
            "dir": "extract/pg6130-images.epub/6130/0.css"
        }
    ]
},
{
    "book2": [
        {
            "dir": "extract/pg6130-images.epub"
        },
        {
            "dir": "extract/pg6130-images.epub/6130/0.css"
        }
    ]
}
]

Your JSON was not valid i used: http://jsonlint.com/ to sort it! 我使用了您的JSON无效: http : //jsonlint.com/对其进行了排序!

Now you should be able to acess the data fine. 现在您应该可以正常访问数据了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM