JSON accessors after ajax request
I've spent the last couple of hours trying to figure this out. I am making an ajax request using JQuery. I get the response in string format and I use
jQuery.parseJSON(response);
to convert it to Object. This is my answer:
{
"columns": ["n"],
"data": [
[{
"extensions": {},
"labels": "http://localhost:7474/db/data/node/168/labels",
"outgoing_relationships": "http://localhost:7474/db/data/node/168/relationships/out",
"traverse": "http://localhost:7474/db/data/node/168/traverse/{returnType}",
"all_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/all/{-list|&|types}",
"property": "http://localhost:7474/db/data/node/168/properties/{key}",
"self": "http://localhost:7474/db/data/node/168",
"properties": "http://localhost:7474/db/data/node/168/properties",
"outgoing_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/out/{-list|&|types}",
"incoming_relationships": "http://localhost:7474/db/data/node/168/relationships/in",
"create_relationship": "http://localhost:7474/db/data/node/168/relationships",
"paged_traverse": "http://localhost:7474/db/data/node/168/paged/traverse/{returnType}{?pageSize,leaseTime}",
"all_relationships": "http://localhost:7474/db/data/node/168/relationships/all",
"incoming_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/in/{-list|&|types}",
"metadata": {
"id": 168,
"labels": []
},
"data": {
"name": "1"
}
}]
]
}
I am trying to access certain elements of this object, but I get nothing. When I try this
var test = json.data;
it works, but how can i access the values ββstored in the metadata. I try this, but I get "undefined":
var test = json.data.metadata.id;
Any idea what I'm missing?
It is an array, so you must use an index
var test = json.data[0][0].metadata.id;
json.data[0]
returns [{...}]
again [{...}][0]
returns {...}
, which has metadata
Object, which, in turn, as a property id
that you need.
json.data is a double array.
This should work:
var test = json.data[0][0].metadata.id;
Try:
var test = json.data[0][0].metadata.id;
json.data
- an array of arrays.