gpt4 book ai didi

javascript - 为什么浏览器向 json 响应添加额外的反斜杠并且 JSON 数组长度错误?

转载 作者:行者123 更新时间:2023-11-30 17:14:40 25 4
gpt4 key购买 nike

这就是我向服务器发出回调请求以获取 JSON 值的方式

$(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 ');
}
});


});

我正在观察服务器控制台中显示的 JSON 响应在 () 中有斜线

{
"json_value": "[{\"contact_phone_no\":\"9876543210\",\"SurCharges\":\"50\",\"vendorname\":\"VC4 Raj\",\"contact_email\":\"rajk@gmail.com\",\"count\":\"0\",\"discount_div\":\"10%\",\"itemid\":\"188\",\"strikeprice_cutoff\":\"90\",\"name\":\"Popcorn Plain salted\",\"contact_address\":\"Kukatpally, Hyderabad\",\"quantity\":\"1\",\"email_id\":\"raj@hmail.com\",\"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\":\"rajk@gmail.com\",\"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\":\"raj@hmail.com\",\"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\"}]"
}

为什么长度显示为 1525 ??

因为上面的 JSON 由两个值组成?? (从contact_phone_no开始每条都是一条新记录)

最佳答案

服务器端存在错误。它不会向您发送一个 JSON 对象图(即嵌套的 JSON 对象),而是向您发送一个包含 JSON 格式对象的字符串。或者换句话说:它向您发送 JSON 内部的 JSON。

要解析数据,您需要以下代码:

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

或者修复服务器端的bug。

关于javascript - 为什么浏览器向 json 响应添加额外的反斜杠并且 JSON 数组长度错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com