Category Archives: JSON

JSON codes

Access JSON

Access JSON content into JavaScript: example var x = ‘{“a”: [‘ + ‘{“name1”: “value1”, “name2”: “value1”},’ + ‘{“name1”: “value2”, “name2”: “value2”} ]}’; var obj = JSON.parse(x); document.getElementById(“id”).innerHTML = obj.a[1].name1 + ” ” + obj.a[1].name2;   Access the first entry in the … Continue reading

Posted in JSON | Leave a comment

Convert JSON content into JavaScript

var x = ‘{ “name1”: “value1”, “name2”: “value1” }’; var obj = JSON.parse(x); document.getElementById(“id”).innerHTML = obj.name1 + ” ” + obj.name2;  

Posted in JSON | Tagged ,

Display JSON array

var x = [ {“name1”: “value1”, “name2”: “value1”}, {“name1”: “value2”, “name2”: “value2″} ] myFunction(x); function myFunction(arr) { var out = ” “; for (var i=0; i < arr.length; i++) { out += arr[i].name1 + arr[i].name2 + ”; } document.getElementById(“id”).innerHTML = … Continue reading

Posted in JSON | Tagged ,

Modify or Set Data

example1 / example2 var x = [ {“name1”: “value1”, “name2”: “value1”}, {” name1″: “value2″, ” name2″: “value2”} ]; x[0].name1=”newValue”; document.getElementById(“id”).innerHTML = x[0].name1 + ” ” + x[0].name2; OR x[0][“name1″]=”newValue”; document.getElementById(“id”).innerHTML = x[0][“name1″] + ” ” + x[0][“name2”];

Posted in JSON | Tagged , ,