首页 > 解决方案 > 无法在php中进行json解码

问题描述

我有像下面这样的字符串我正在尝试对其进行 json_decode() 但它给了我空值

{
"products": [{
    "product_id": 6,
    "product_name": "Reusable Baby Cloth Diaper with 5 Layered Baby Insert Pads (Pack of 2), id:(6)",
    "quantity": Reusable Baby Cloth Diaper with 5 Layered Baby Insert Pads (Pack of 2), id:(6),
    "strikeprice": ,"cart_total": 120,"delivery_charge": 10,"delivery_discount": ,"discount": 20,"gullack_discount": 10,"gullack_points_used": 20,"max_points_usable_original": ,"final_price": 200,"gullack_point_win": 200,"product_image": "","referrer": null,"share_points_given": 0,"order_status": 1"max_points_usable":0}],"orderDetails": {"address_id": 406,"phone": "9284759237","payment_mode": ,"successCallbackUrl": "http://localhost:4200/ordersuccess/{orderId}","failedCallbackUrl": "http://localhost:4200/placeorder#paymentFailed","campaign_id": "NA","query": "","offer_id": null,"query_string": ""}
}

提前致谢

标签: phpjson

解决方案


这部分 JSON 无效:

"quantity": Reusable Baby Cloth Diaper with 5 Layered Baby Insert Pads (Pack of 2), id:(6),

我不确定为什么数量是字符串而不是数字。但既然是这样,它就需要引号:

"quantity": "Reusable Baby Cloth Diaper with 5 Layered Baby Insert Pads (Pack of 2), id:(6)",

您应该修复创建 JSON 的代码。如果它使用 JSON 库,这种类型的错误将是不可能的,因此它必须以一种特别的方式构造 JSON,并且做错了。


推荐阅读