I am fairly new to JSON and jquery and javascript. Using this in php. I have a script that is being called by AJAX that is returning the following when I run console.log
var resp = $.parseJSON(data);
console.log(JSON.stringify(resp));
Returns the followingin the console.:
{"status":"OK","action":["SHIPADDPOST","SHIPADDPOST=Shipment saved ADD"],"data":{"shipmentId":"76"},"timestamp":1444069313,"generationTime":"298ms"}
My question is the following: How can I retrieve (in javascript) the value 76 (which is "shipmentID")?
You could use JSON.parse() but it might not be supported by all browsers.
Here an example:
var obj = JSON.parse(data);
alert(obj.data.shipmentId);
Check:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse for more info on this method and compatibility.
You could do the JSON.parse and extract the shipmentId as below.
var shop = '{"status":"OK","action":["SHIPADDPOST","SHIPADDPOST=Shipment saved ADD"],"data":{"shipmentId":"76"},"timestamp":1444069313,"generationTime":"298ms"}';
var obj = JSON.parse(shop);
alert(obj.data.shipmentId);