0

I'm having trouble with JSON. I made this in PHP and I'm sending it to my JavaScript, but I can't get the values.

[
   {
      "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?

Lemex
  • 3,772
  • 14
  • 53
  • 87
jambuls
  • 111
  • 11
  • 5
    The JSON in the question is not a valid JSON: it's missing closing square brackets for each book. – Aleks G May 01 '13 at 10:02
  • 1
    Also, it lacks commas between array members. – punund May 01 '13 at 10:04
  • possible duplicate of [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value) – Felix Kling May 01 '13 at 10:04
  • 1
    You should look into [`json_encode()`](http://php.net/manual/en/function.json-encode.php) to get a proper JSON-encoding of your data in PHP. – Sirko May 01 '13 at 10:06
  • @AleksG: That's an answer, not a comment. Please post it as such; that way, OP will be able to mark it as the accepted answer. – Martijn May 01 '13 at 10:06
  • Please show the PHP code that you used to create it. Your JSON code is invalid so you're clearly not generating it properly. HINT: did you use php's `json_encode()` function? – Spudley May 01 '13 at 10:06

4 Answers4

1

First you need to validate your json, i have validate your json it gives error. In your json dIr is id. You have defined 3 dir id for same object this may be error.

Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
0

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.

You don't need to do JSON.parse you can simply do

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

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"];
Ally
  • 4,894
  • 8
  • 37
  • 45
0

The code shown in the question is not a valid JSON. There are missing closing square brackets for each of the book arrays and (thanks to @punund) a missing comma between array members. The correct JSON would be this:

[
   {
      "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. The following PHP will produce valid JSON for your scenario:

<?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);

?>
Aleks G
  • 56,435
  • 29
  • 168
  • 265
0
[
{
    "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!

Now you should be able to acess the data fine.

Lemex
  • 3,772
  • 14
  • 53
  • 87