2016-11-23 18 views
0

我有以下字符串值的變量:的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":[]}}}} 
+0

如果這實際上您的字符串變量的內容,那麼它應該工作。你確定你在使用'JSON.parse'嗎? – qxz

回答

3

由於您的原始變量是一個字符串而不是對象,因此它將作爲字符串插入到新對象中。在\」問題只是逃避引號,以免過早關閉串

將您的字符串轉換成JSON對象,將其添加到新的對象:

JSON.parse(json_string); 
0

您的變量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由單引號,因爲它是一個字符串