0

I have a response from an Ajax POST request which looks like this:

{
    "columns": [
        "r"
    ],
    "data": [
        [
            {
                "extensions": {},
                "start": "http://localhost:7474/db/data/node/2762",
                "property": "http://localhost:7474/db/data/relationship/2709/properties/{key}",
                "self": "http://localhost:7474/db/data/relationship/2709",
                "properties": "http://localhost:7474/db/data/relationship/2709/properties",
                "type": "IS_CONNECTED_WITH",
                "end": "http://localhost:7474/db/data/node/2763",
                "metadata": {
                    "id": 2709,
                    "type": "IS_CONNECTED_WITH"
                },
                "data": {
                    "FOC_Type": "IRU",
                    "DuctType": "IRU",
                    "TrenchType": "IRU",
                    "linestringId": "53805"
                }
            }
        ]
    ]
}

The above is a string. What I am trying to access is the elements: "FOC_Type":"IRU","DuctType":"IRU","TrenchType":"IRU","linestringId":"53805".

I transform the string to JSON like this:

  var response = JSON.parse(response);

and then I try to access some of the values like this:

  var dataEquip = response[Object.keys(response)[Object.keys(response).length - 1]] // get the data from json obj

  var komvosName = dataEquip[0][2];

But I can not make it work.

I found a work around in which I don't convert the response to JSON format and I play with the string. But that's not nice. I would appreciate if someone could show me what I do wrong.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
user1919
  • 3,818
  • 17
  • 62
  • 97
  • 1
    `I transform the string to JSON`, JSON is a string. You are transforming to a javascript object – Matt Burland Mar 13 '15 at 14:34
  • The array returned by Object.keys can be in any arbitrary order - it is not guaranteed to be in the same order as the keys in your JSON. – radiaph Mar 13 '15 at 14:40

1 Answers1

1

What about doing :

var responseJSON = JSON.parse(response);
var dataEquip = responseJSON ['data'] // get the data from json obj
var komvosName = dataEquip['TrenchType'];

a JSON object (responseJSON ) is nothing more than an associative array.

yunandtidus
  • 3,847
  • 3
  • 29
  • 42