0

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

[{"node_title":"king","nid":"6","Body":"

这是国王

\n"},{"node_title":"sree","nid":"3","Body":"

ggbderfrtgrsf

\n"}]

请帮忙?

4

2 回答 2

1
 <!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

于 2012-11-09T07:02:24.863 回答
0

使用敲除.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"}]));​

JSFiddle示例

于 2012-11-09T07:21:17.240 回答