如何将div的文本作为json字符串放置?
这个问题在这里已经有了答案:
如果您尝试从JSON字符串中创建对象,请查找JSON.parse()
:
var out = [{
"name": "Leopardi",
"children": [{
"name": "Vita",
"children": [{
"name": "Ubriacone"
}]
}, {
"name": "Poesie",
"children": [{
"name": "L'infinito"
}]
}]
}];
$('#myMap').text(JSON.stringify(out[0], null, 2));
var root = JSON.parse( $('#myMap').text() ); // <-- JSON.parse
console.log('root.name:', root.name); // <-- can treat 'root' as object
#myMap {
border: 1px solid;
padding: .5em;
font-family: monospace;
white-space: pre;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
MyMap:
<div id="myMap"></div>
链接地址: http://www.djcxy.com/p/8447.html