nilesh nilesh - 3 months ago 11
PHP Question

Read JSON response object in php/js

I want to display particular values of response object in HTML. But I am not able to display those values.

With

json.stringify(data)
I can convert response to string. I want to show Status and StatusCode to user. How can I get these values?

demo

<script>
$jq.ajax({
url: 'api url',
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
success: function(data) {
var JSONString =JSON.stringify(data);

//var json = $jq.parseJSON(data);
$jq.each(data.ShipmentData, function(index, value){
// alert(JSON.stringify(value));
//console.log(value);
//console.log(JSON.stringify(Shipment));
});

},
error: function() {
alert("FAIL");
}
});
<script>

Answer

No need to stringify your data!

You just have to access the object properties. Like this:

$jq.each(data.ShipmentData, function(){
    console.log(this.Shipment.Status.Status);
    console.log(this.Shipment.Status.StatusCode);
});

where this is the current object of the each iteration