-2

Here the data I get as json from the server?

{id: 1, no: "CP101", pack: "PH"}

How can I read it using javascript? when I use

var obj = JSON.parse('{id: 1, no: "CP101", pack: "PH"}');
document.getElementById("demo").innerHTML = obj.id + ", " + obj.no;

Gives me No output.

Rajnish
  • 13
  • 9

3 Answers3

0

you should add some html sympol like this: document.getElementById('domId').innerHTML="<div>+"obj.id+obj.no+"</div>"; because the innerHTML is content like string

Godman
  • 1
  • 2
0

you must be well formatted your parsing data something like below-

var obj = JSON.parse('{"id": 1, "no": "CP101", "pack": "PH"}');

Tanjeeb Ahsan
  • 87
  • 1
  • 7
0

Your Object is not stringified and you can not use JSON.parse.

var obj = {
  id: 1,
  no: "CP101",
  pack: "PH"
};
document.getElementById("demo").innerHTML = obj.id + ", " + obj.no;

console.log("Normal Object", obj, "Type of it is", typeof obj);

var myJSON = JSON.stringify(obj);
console.log("Stringified Object", myJSON, "Type of it is", typeof myJSON);
<div id="demo"></div>

Read here and here

Saeed
  • 5,413
  • 3
  • 26
  • 40