我有以下字符串值的變量:的Javascript JSON字段具有」後的各推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).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.parse'嗎? – qxz