user663724
user663724

Reputation:

why browser is adding extra backward slashes to the json Response and also JSON array length is wrong?

This is the way i am making a callback request to the server for a JSON Value

$(document).on('click', '.currentOrderRow', function(event ) {
var orderid = $(this).attr("id_data");

$.ajax({
        type: 'GET',
        url: url+'/OMS/oms1/getOrderdetails?orderid='+orderid,
        jsonpCallback: 'jsonCallback',
        cache: true,
        dataType: 'jsonp',
        jsonp: false,
        success: function (response) {
    alert(response.json_value.length);
            console.log('response is'+JSON.stringify(response));

        },
        error: function (e) {
            alert('Into error ');
        }
    });


});

I am observing while the JSON response which is shown in the server console is having slashes within in ()

{
    "json_value": "[{\"contact_phone_no\":\"9876543210\",\"SurCharges\":\"50\",\"vendorname\":\"VC4 Raj\",\"contact_email\":\"[email protected]\",\"count\":\"0\",\"discount_div\":\"10%\",\"itemid\":\"188\",\"strikeprice_cutoff\":\"90\",\"name\":\"Popcorn Plain salted\",\"contact_address\":\"Kukatpally, Hyderabad\",\"quantity\":\"1\",\"email_id\":\"[email protected]\",\"date_time\":\"14:10:37\",\"toppings\":[{\"name\":\"Quantity  1\",\"value\":[\"Honey with Chocolate Sauce 10 ML\",\"None\",\"None\",\"None\",\"None\",\"None\",\"None\",\"None\"]}],\"screen\":\"SCR-3\",\"seat_num\":\"D12\",\"customer_name\":\"Ganesh\",\"image\":\"images/icon-print.png\",\"contact_person\":\"Kiran\",\"item_description\":\"Item description\",\"vendor_id\":\"9\",\"crusts\":[],\"Vat\":\"70\",\"customer_mobil\":\"9090987878\",\"price\":\"115\",\"mobile_number\":\"1234567898\",\"ServicesCharges\":\"50\",\"orderid\":\"14101337\"},{\"contact_phone_no\":\"9876543210\",\"SurCharges\":\"50\",\"vendorname\":\"VC4 Raj\",\"contact_email\":\"[email protected]\",\"count\":\"0\",\"discount_div\":\"10Rs\",\"itemid\":\"194\",\"strikeprice_cutoff\":\"110\",\"name\":\"Popcorn Regular   300 ML Fountain Apple\",\"contact_address\":\"Kukatpally, Hyderabad\",\"quantity\":\"1\",\"email_id\":\"[email protected]\",\"date_time\":\"14:10:37\",\"toppings\":[{\"name\":\"Quantity  1\",\"value\":[\"Chocolate\",\"Vanila\"]}],\"screen\":\"SCR-3\",\"seat_num\":\"D12\",\"customer_name\":\"Ganesh\",\"image\":\"images/icon-print.png\",\"contact_person\":\"Kiran\",\"item_description\":\"Item description\",\"vendor_id\":\"9\",\"crusts\":[{\"name\":\"Quantity  1\",\"value\":[\"Butter scotch\"]}],\"Vat\":\"70\",\"customer_mobil\":\"9090987878\",\"price\":\"275\",\"mobile_number\":\"1234567898\",\"ServicesCharges\":\"50\",\"orderid\":\"14101337\"}]"
}

And why the length is being shown as 1525 ??

Because the above JSON is consisting of two values within it ?? (Starting from contact_phone_no each is a new record )

Upvotes: 1

Views: 1194

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328614

There is a bug on the server side. Instead of sending you a JSON object graph (i.e. nested JSON objects), it sends you a single string which contains objects in JSON format. Or to put it another way: It send you JSON inside of JSON.

To parse the data, you need this code:

var data = JSON.parse(response.json_value);

Or fix the bug on the server side.

Upvotes: 2

Related Questions