Javascript JSON字段有\";在每次推送JSON字符串之后

Javascript JSON字段有\";在每次推送JSON字符串之后,javascript,json,Javascript,Json,我有一个具有以下字符串值的变量: {"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

我有一个具有以下字符串值的变量:

{"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变量的以下输出,正如您所看到的,
\“
其中应该只有
,还有
“元素”:[“{\”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);

由于原始变量是字符串而不是对象,因此它将作为字符串插入到新对象中。问题只是转义引号,以免过早关闭字符串

将字符串转换为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
被单引号包围,因为它是一个字符串您的变量
elementArrayItemJson
是一个无效的JSON对象。它有两个根元素,因此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
被单引号包围,因为它是一个字符串

如果这实际上是字符串变量的内容,那么它应该工作。您确定您使用的是
JSON.parse
?如果这实际上是字符串变量的内容,那么它应该工作。您确定吗您使用的是
JSON.parse