I have this example where I am trying to access json value, but it does not even produces any alert. What is the problem?
My JSON
{
"response": [
{
"id": "0",
"elementName": "osname",
"isEqual": true,
"isPrasentinXml1": true,
"isPrasentinXml2": true,
"attribute": [
{
"name": "osname",
"firstValue": "Linux\u000a",
"secondValue": "SunOs\u000a"
}
]
},
{
"id": "1",
"elementName": "hostname",
"isEqual": false,
"isPrasentinXml1": true,
"isPrasentinXml2": true,
"attribute": [
{
"name": "hostname",
"firstValue": "estilo\u000a",
"secondValue": "buckeye.informatica.com\u000a"
}
]
}
]
}
I want to fetch Linux\u000a and SunOs\u000a, so i wrote
alert(compareData.response[0].attribute[0].firstValue+", "+compareData.response[0].attribute[0].secondValue);
Note: compareData Is where my data is in actual code
Your error was that you have quotes around your JSON and it was treated as a string. Also you forgot to replace
compareDatavariable name withjsonobjin the secondalert. Try the fiddle below, it seems it is what you want.jsFiddle: http://jsfiddle.net/Dna9H/6/
EDIT:
If your JSON is really represented by the string take a look at Michael Sagalovich solution.