Jaydeep Gondaliya Jaydeep Gondaliya - 4 months ago 31
jQuery Question

Get jsondata value in javascript

JSON

var jsondata={"id": "10", "skills": "english", "post": "devloper", "emp_name": "jaydeep","timestemp":"10:45"}


I am trying to get each element key and value:

javascript

..
}).done(function(data){
console(data['post']);
});


Expected Output :

emp_name = jaydeep

post = devloper

I am getting undefined in console. WHY? I tried data.post, i tried loop but no success..

Answer

You can use basic JS too to attain this.

// property is an optional parameter
function disp(obj, property) {
  var prop;
  if (property) {
    obj[property] && (console.log(obj[property]));
  } else {
    for (prop in obj) {
      console.log(prop + " = " + obj[prop])
    }
  }
}
var jsondata = {
  "id": "10",
  "skills": "english",
  "post": "devloper",
  "emp_name": "jaydeep",
  "timestemp": "10:45"
}

//disp(jsondata, "post");
disp(jsondata);