gpt4 book ai didi

javascript - JSON 语法错误 - JSON.parse

转载 作者:行者123 更新时间:2023-11-28 17:24:16 26 4
gpt4 key购买 nike

我在尝试解析此代码时遇到错误(如果格式不正确,请道歉 - 我是新手):

 {
"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}]
}

Error message: Uncaught SyntaxError: Unexpected token d in JSON at position 272

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

谢谢!

最佳答案

修复此行:

"customerName": "${order_res.customer_name}", //string

在您的代码中,如果 order_res.customer_name 等于“konichiwa”,那么您将得到:

"customerName": konichiwa

但是你需要:

"customerName": "konichiwa"

另外,如果你有这样的数组:

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

你得到:

"itemName": [hello, world]

而不是:

"itemName": ["hello", "world"]

关于javascript - JSON 语法错误 - JSON.parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51961142/

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