php JSON.parse无法解析,出现无法处理项目语法错误:JSON后位置2处出现意外的非空白字符

fcipmucu  于 2022-12-25  发布在  PHP
关注(0)|答案(1)|浏览(317)

我有这段JavaScript代码,当尝试收集details值时,JSON中的所有其他值都被正确传递到各自的字段,但details字段失败,出现以下错误。我尝试了details对象的各种数组和不同组合
第一个月
这是我的PHP和JavaScript代码,它在这一行失败。

let items = JSON.parse(data.details);

但是,console.log(data.details)正确打印详细信息字段值。
JavaScript语言

function processOrderItems(data) {
    try {
        console.log(data);
        console.log(data.details);

        if (data && data.details) {
            let orderItems = "";
            console.log('oujyiu');

            let items = JSON.parse(data.details);
            console.log(items);
            console.log('345353');

            items.forEach((item) => {
                orderItems = orderItems + `${item.c} X ${item.p}` + "\n";
            });
            data.orderItems = orderItems;
        }
    } catch (err) {
        console.log("Unable to Process items", err);
    }
}

console.log(数据)输出

{
    "tagId": "Ask_for_payment",
    "tags": "Shipping_Pending",
    "phone": "917980730935",
    "encoded-payload": "eyJ0YWdJZCI",
    "name": "#",
    "customerName": "Ravi",
    "status": "confirmed",
    "fulfillmentStatus": "unfulfilled",
    "paymentStatus": "Paid",
    "id": "9024033",
    "ids": "9024033true",
    "createdOn": "July 25, 2022",
    "trackingNumber": "-NA-",
    "address": "Not avialable",
    "pd-clickable": "pd-clickable-order-div",
    "lineItems": "",
    "details": "65ttgtest prodct",
    "hide-line-items-div": "pd-hidden",
    "hiddenLineItems": "",
    "currency": "₹",
    "total": "1310.98",
    "collapse-icon": "true",
    "is_hide": "",
    "expand-icon": "true",
    "readableAddress": "Not avialable",
    "hide-div": "pd-hidden",
    "card-index": 30,
    "can-hide": ""
}

PHP语言

$finaldataa .= '{"tagId":"'.$fertrr.'","tags":"'.$djjrjr.'","phone":"'.$phone_noyyy.'","encoded-payload":"kkjhjjhhh","name":"#'.$order_number.'","customerName":"'.$customer_name.'","status":"Confirmed","fulfillmentStatus":"'.$kkekffddf.'","paymentStatus":"Paid","id":"'.$idggg.'","ids":"'.$idggg.'","createdOn":"'.$time_created.'","trackingNumber":"'.$tracking_no.'","address":"Not avialable","pd-clickable":"true","lineItems":"65ttg'.$prod_title.'","details":"65ttg'.$prod_title.'","hide-line-items-div":"false","hiddenLineItems":"false","currency":"'.$s_currency.'","total":"'.$amount.'","collapse-icon":"true","is_hide":"true","expand-icon":"true"},';
dw1jzc5e

dw1jzc5e1#

也许您生成了错误的JSON,因为您是手动生成的。
PHP有一个生成JSON的内置函数-https://www.php.net/manual/en/function.json-encode.php
因此,使用内置的json_encode函数代替字符串连接。
下面是一个例子:

$items = [
    ['tagId' => 'Ask_for_payment 1', /* your other props */],
    ['tagId' => 'Ask_for_payment 2', /* your other props */],
];

$json = json_encode($items, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
echo $json; // Will print: [{"tagId":"Ask_for_payment 1"},{"tagId":"Ask_for_payment 2"}]

相关问题