json到html的代码

时间:2012-11-09 06:55:52

标签: json html-parsing

我有以下json格式。我想在html中显示每个字段。如何从json检索值到html

  

[{“node_title”:“king”,“nid”:“6”,“Body”:“

这是王

\ n”},{“node_title”: “SREE”, “NID”: “3”, “体”: “

ggbderfrtgrsf

\ n” 个}]

请帮帮忙?

2 个答案:

答案 0 :(得分:0)

 <!DOCTYPE html>
<html>
<body>
<h2>JSON Object Creation in JavaScript</h2>

<p>
Name: <span id="jname"></span><br />
Age: <span id="jage"></span><br />
Address: <span id="jstreet"></span><br />
Phone: <span id="jphone"></span><br />
</p>

<script>
var JSONObject= {
"name":"John Johnson",
"street":"Oslo West 555",
"age":33,
"phone":"555 1234567"};
document.getElementById("jname").innerHTML=JSONObject.name
document.getElementById("jage").innerHTML=JSONObject.age
document.getElementById("jstreet").innerHTML=JSONObject.street
document.getElementById("jphone").innerHTML=JSONObject.phone
</script>

</body>
</html>

使用这样的东西。这是w3Schools

答案 1 :(得分:0)

使用knockout.js

<div>
   <!-- ko foreach: values -->
    <p>node_title: <span data-bind='text: node_title' ></span></p> 
    <p>nid: <span data-bind='text: nid'></span></p> 
    <p>Body: <span data-bind='text: Body'></span></p>
    <!-- /ko -->    
</div>​     

javascript

var ViewModel = function(data) {
    this.values = ko.observableArray(data);
};

ko.applyBindings(new ViewModel([{"node_title":"king","nid":"6","Body":"This is king\n"},{"node_title":"sree","nid":"3","Body":"ggbderfrtgrsf\n"}]));​

JSFiddleExample