0

i am parsing an json string usin javascript

    {
      "head":   {
        "example": "0"
         },
      "res":   {
        "@test": "121",
        "@found": "5"
     }
}

i am getting the http response from the remote site and i am using javascript to extract the @found value .i used the below code .

 var json=eval('('+request.responseText+')');
        alert(json.head.res.@found);

gives me a null value can u please tell me how can i parse this.

Ramesh
  • 2,295
  • 5
  • 35
  • 64

5 Answers5

3
alert(json.head.res['@found']);

@ is not valid to start a variable name, so you have to use bracket notation. Or change the variable name to be valid.

Basically, in regular expression form: [a-zA-Z_$][0-9a-zA-Z_$]*. In other words, the first character can be a letter or _ or $, and the other characters can be letters or _ or $ or numbers.

What characters are valid for JavaScript variable names?


Per Topera's answer head and res are on the same level in your object not nested.
Community
  • 1
  • 1
Joe
  • 80,724
  • 18
  • 127
  • 145
1

Attribute "res" is not inside "head" attribute.

Try this: json.res['@found']

Topera
  • 12,223
  • 15
  • 67
  • 104
0
alert(json.res['@found']);

res is not inside head.

bluish
  • 26,356
  • 27
  • 122
  • 180
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
0

Try json.res['@found']. JavaScript object properties that are not valid variable names cannot be accessed using dot notation. Also, as noted by others, @found is not in head.

abesto
  • 2,331
  • 16
  • 28
0

fix these 3 things :

  • @ is not a valid variable name character. you should consider the [] syntax instead of the "dot" one.
  • res is not in head.
  • please don't use eval. I strongly advise you to use JSON.parse instead of eval
var json = JSON.parse(request.responseText);
alert(json.res['@found']);
gion_13
  • 41,171
  • 10
  • 96
  • 108
  • thanks for your advice and the json is converted from xml so it has the name starts with @ .i used json-lib to convert it in java . – Ramesh Oct 11 '11 at 13:35