如何将以下字符串转换为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>\",
}
]
}
答案 0 :(得分:0)
您粘贴的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>"
}
]
}
]
}