Adam12344 Adam12344 - 5 months ago 12
HTML Question

Parsing JSON Response in HTML and Javascript

My web app returns a JSON object. I am simply trying to write the result to a page and the best I can get is 'undefined'.

<p id="demo"></p>

<script>
var text= [
{
"name": "xx",
"street": 65.2067810799497698,
"phone": 134.2967768940979501
}
];


alert(text.name)

document.getElementById("demo").innerHTML =
text.name
;
</script>

Answer

You dont need to parse anything, just change text to text[0] whenever you are trying to get your value (name, street...) from the first index which is 0

<p id="demo"></p>

<script>
 var text= [
  {
    "name": "xx",
    "street": 65.2067810799497698,
    "phone": 134.2967768940979501
  }
];

alert(text[0].name)

document.getElementById("demo").innerHTML = text[0].name 

</script>