1

This is my JSON from PHP:

{"data" :
    [
        {
            "_id" : {
                "$id" : "4f977259b1445dce24000000"
            },
            "headline" : "asdfasdf",
            "date" : {
                "sec" : 1333584000,
                "usec" : 0
            },
            "text":"asdfasdfas"
        }
    ]
}

In Javascript I want to use the values and it works fine with

obj = JSON.parse(request);
console.log(obj.data[i].headline);

But how do I get the ObjectId?

It does not work like this:

console.log(obj.data[i]._id.$id);
T. Junghans
  • 11,385
  • 7
  • 52
  • 75
user1354743
  • 407
  • 3
  • 7
  • 20
  • I can verify that the answers gillesc and reach4thelasers work (at least in Safari). You may also want to have a look at http://stackoverflow.com/questions/2940424/valid-javascript-object-property-names . – T. Junghans Apr 25 '12 at 15:11

2 Answers2

2

It seems to work as expected when I try it http://jsfiddle.net/2CSWr/

console.log(json.data[0]._id.$id);​

does output the right value

GillesC
  • 10,647
  • 3
  • 40
  • 55
0

This works for me:

console.log(obj.data[0]._id['$id']);
reach4thelasers
  • 26,181
  • 22
  • 92
  • 123