Jeremy Miller Jeremy Miller - 2 months ago 6
Javascript Question

How can I easily have a javascript function to parse out specific objects from json string

I would like to pull out each of these fields and put them into a their own variable in javascript

{
"did": "00022B9A000000010001",
"ps": -1,
"hostName": "zs-dptpre-002.mr.corp.l.com",
"ipAddress": "10.4.225.29"
}

Answer

You seem to want something very very plain ...

var jsonString = '{"did": "00022B9A000000010001","ps": -1,"hostName": "zs-  dptpre-002.mr.corp.l.com","ipAddress": "10.4.225.29"}';
var obj = JSON.parse(jsonString);

var did = obj.did;
var ps = obj.ps;
var hostName = obj.hostName;
var ipAddress = obj.ipAddress;

console.log(did);
console.log(obj);
Comments