[英]How to convert string to nested json array
如何將以下字符串轉換為json數組對象。 jQuery.parseJSON()
在我的情況下不起作用
{
label: \"test 1\",
expanded: true,
items: [
{
selected: true,
label: \"<a onclick=
'sendEndpoint("https://contoso/Test1/","List 1");' > test12 1 < /a>\"
},{
label: \ "<a onclick='send("https://contoso/Test11/","test222");'>
test222 < /a>\"
},{
label: \ "<a onclick='send("https://contoso/test33/","testt 3");'>testt 3</a>\"
},{
label: \ "tesst 2\", items: [ { label: \"<a onclick='send("https://contoso/tessst","tesst 1 & quot;);'>tesst</a>\",
}
]
}
您粘貼的JSON字符串中有幾個問題。
最終“標簽”末尾有一個額外的“,”。可以通過以下方式修改邏輯:默認情況下,在JSON鍵值的末尾不添加“,”,並且僅在有JSON對象中的另一個鍵值。
通過使用jQuery.parseJSON()
您的意圖是將提供的字符串解析為JSON對象。 如果是這樣,則鍵和值都應用“”(雙引號)引起來。
解決這兩個問題后,最終字符串將是
{ \"label\": \"test 1\", \"expanded\": true, \"items\": [ { \"selected\": true, \"label\": \"<a onclick='sendEndpoint("https://contoso/Test1/","List 1");'>test12 1</a>\" }, { \"label\": \"<a onclick='send("https://contoso/Test11/","test222");'>test222</a>\" }, { \"label\": \"<a onclick='send("https://contoso/test33/","testt 3");'>testt 3</a>\" },{ \"label\": \"tesst 2\", \"items\": [ { \"label\": \"<a onclick='send("https://contoso/tessst","tesst 1");'>tesst</a>\", } ] } ] }
{ "label":"test 1", "expanded":true, "items":[ { "selected":true, "label":"<a onclick='sendEndpoint("https://contoso/Test1/","List 1");'>test12 1</a>" }, { "label":"<a onclick='send("https://contoso/Test11/","test222");'>test222</a>" }, { "label":"<a onclick='send("https://contoso/test33/","testt 3");'>testt 3</a>" }, { "label":"tesst 2", "items":[ { "label":"<a onclick='send("https://contoso/tessst","tesst 1");'>tesst</a>" } ] } ] }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.