[英]Javascript JSON field has \" after each pushed JSON string
我有一个带有以下字符串值的变量:
{"title":"Zebra Long Maxi Dress","subtitle":"Unique and comfortable Maxi dress that is easy to wear and fun to be seen in.","quantity":1,"price":150,"currency":"RON","image_url":"https://dl.airtable.com/1gGAho3dQ8uQfWpa5pEV_anne-1-(8).png"},{"title":"Blue fan dress","subtitle":"This blue fan dress will blow people away. With its custom cut design you will always stand out.\n","quantity":1,"price":100,"currency":"RON","image_url":"https://dl.airtable.com/oerRxGtZRuqnI9afPmn0_anne-1-(35).png"}
我将其推送到以下结构的元素部分,如下所示:
var messageData = {
recipient : {
id: sender
},
message:{
attachment:{
type:"template",
payload:{
template_type:"receipt",
recipient_name: clientName,
order_number: orderNumber,
currency:"RON",
payment_method:"Rambus",
//order_url:"http://www.facebook.com",
//timestamp:"1428444852",
elements:[],
address:{
street_1:address,
city:"Bucharest",
postal_code:"0",
state:"_",
country:"RO"
},
summary:{
subtotal:0,
shipping_cost:0,
total_tax:0,
total_cost:totalCost
},
adjustments:[]
} // payload
} // attachment
} // message
}; // messageData
messageData.message.attachment.payload.elements.push(JSON.parse(elementArrayItemJson));
而且我得到了messageData变量的以下输出,如您所见,有一个\\"
应该只有一个"
,还有"elements":["{\\"title\\":\\"Zebra Long Maxi Dress\\"
具有额外的"
我不确定如何使JSON看起来正确。
{"recipient":{"id":"1588309797861804"},"message":{"attachment":{"type":"template","payload":{"template_type":"receipt","recipient_name":"Ethan Richardson","order_number":"70128","currency":"RON","payment_method":"Rambus","elements":["{\"title\":\"Zebra Long Maxi Dress\",\"subtitle\":\"Unique and comfortable Maxi dress that is easy to wear and fun to be seen in.\",\"quantity\":1,\"price\":150,\"currency\":\"RON\",\"image_url\":\"https://dl.airtable.com/1gGAho3dQ8uQfWpa5pEV_anne-1-(8).png\"}","{\"title\":\"Blue fan dress\",\"subtitle\":\"This blue fan dress will blow people away. With its custom cut design you will always stand out.\\n\",\"quantity\":1,\"price\":100,\"currency\":\"RON\",\"image_url\":\"https://dl.airtable.com/oerRxGtZRuqnI9afPmn0_anne-1-(35).png\"}"],"address":{"street_1":"Flat 35 Rossetti Court Byron Road","city":"Bucharest","postal_code":"0","state":"_","country":"RO"},"summary":{"subtotal":0,"shipping_cost":0,"total_tax":0,"total_cost":250},"adjustments":[]}}}}
由于您的原始变量是字符串而不是对象,因此它将作为字符串插入到新对象中。 \\“问题只是将引号转义,以免过早关闭字符串。
将您的字符串转换为JSON对象,以将其添加到新对象中:
JSON.parse(json_string);
您的变量elementArrayItemJson
是无效的JSON对象。 它有2个元素,因此JSON.parse会被卷进由于语法错误,
。
字符串化后,当前字符串如下所示:
{
"title": "Zebra Long Maxi Dress",
"subtitle": "Unique and comfortable Maxi dress that is easy to wear and fun to be seen in.",
"quantity": 1,
"price": 150,
"currency": "RON",
"image_url": "https://dl.airtable.com/1gGAho3dQ8uQfWpa5pEV_anne-1-(8).png"
}, {
"title": "Blue fan dress",
"subtitle": "This blue fan dress will blow people away. With its custom cut design you will always stand out.\n",
"quantity": 1,
"price": 100,
"currency": "RON",
"image_url": "https://dl.airtable.com/oerRxGtZRuqnI9afPmn0_anne-1-(35).png"
}
},{
表明它在您的字符串中包含2个未命名的元素。 因此,您可以执行以下操作来拆分字符串:
elementArrayItemJson.replace('},{', '}\n{');
然后按新行(\\ n)分割以获取有效JSON字符串数组:
newElementArrayItemJson.split('\n');
然后,您可以使用JSON.parse(newElementArrayItemJson[0])
推送每个元素
此解决方案可以简化很多,但是会显示错误。
编辑:假设您的变量elementArrayItemJson
引号引起来,因为它是一个字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.