JSON语法错误-JSON.parse

时间:2018-08-22 06:46:34

标签: javascript json

我在尝试解析此代码时遇到错误(如果不正确,请对格式表示歉意-我是新来的人):

 {
     "message": "Please press 1 or 2 to choose the item from your order",
     "totalPrice":  ${order_res.total_price}, //num
     "OrderNum": ${order_res.order_number}, //num
     "orderID": ${order_res.order_id}, //num
     "customerName": ${order_res.customer_name}, //string
     "itemId": [${id}],
     "itemName": [${items}],
     "itemPrice": [${price}]                      
 }
  

错误消息:未捕获的语法错误:JSON中位置272处的意外令牌d

我正在发送此JSON作为响应,现在在我添加了最后三项之前,在后端接收到此响应时,正在解析该响应,但是由于它们的格式相同,所以我不确定为什么它出错了,有人可以帮忙吗我用他们更有经验的眼睛好吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

修复此行:

var result = [
{
   "storeId": "4543",
   "type": "full",
   "overSerializedItems": [3548240,91511753,345555],
},
{
   "storeId": "5462",
   "type": "half",
   "overSerializedItems": [2548240,9566666],
}
];

 var trans = result.map(w => {
  return {
     storeId: w.storeId,
     type: w.type,
     overSerializedItems: w.overSerializedItems.map(x => { return {storeId: x.toString()} })
  }
})

在您的代码中,如果"customerName": "${order_res.customer_name}", //string 等于“ konichiwa”,则您将得到:

order_res.customer_name

但是您需要:

"customerName": konichiwa

此外,如果您有这样的数组:

"customerName": "konichiwa"

您得到:

"itemName": [${['hello', 'world']}]

代替:

"itemName": [hello, world]