How to place text of a div as a json string?

This question already has an answer here:

  • How can I pretty-print JSON using JavaScript? 20 answers

  • 如果您尝试从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/8448.html

    上一篇: JSON.parse和JSON.stringify

    下一篇: 如何将div的文本作为json字符串放置?