[英]How to get JSON and show it with unordered list in HTML?
I have JSON which looks like: 我有看起来像的JSON:
{
"fID": "00202020243123",
"name": "John Doe",
"List": ["Father", "Brother", "Cousin"]
}
I'm rendering this JSON element from my model, and inside of html, I can simply see the contents of the JSON. 我正在从模型中渲染此JSON元素,并且在html内,我可以简单地看到JSON的内容。 However, when I try to do it in a script, it does nothing.
但是,当我尝试在脚本中执行此操作时,它什么也不做。
What I'm trying to do is to take the List attribute and show it in unordered list in HTML. 我想做的是获取List属性,并在HTML的无序列表中显示它。
Here is what i tried so far: 这是我到目前为止尝试过的:
<script>
$("input[name=loadmyJson]").click(function() {//on button click
var items = [];
var myJ = ${json}; //Json element i created in my controller
var myVar = myJ.parseJSON();
$.each(data, function(myVar) {
items.push('<li>' + myVar.List + '</li>');
});
$('#myList').append( items.join('') );//myList is my unordered list's id.
)};
</script>
EDIT: Do i need to add something for parseJSON()
func? 编辑:我需要为
parseJSON()
函数添加一些东西吗?
I would seriously look into a templating engine like json2html (which specializes in converting json to html) for jquery. 我会认真研究jquery的模板引擎,例如json2html (专门将json转换为html)。 Makes life much easier than trying to write your own methods to build html from a source json object.
比起尝试编写自己的方法以从源json对象构建html而言,使生活容易得多。
Note that I changed the List from an array of strings to objects instead (makes it easier to access) 请注意,我将列表从字符串数组改为对象(使其更易于访问)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://json2html.com/js/jquery.json2html-2.3-min.js"></script>
<div id='list'></div>
<script>
var json =
{
"fID": "00202020243123",
"name": "John Doe",
"List": [{label:"Father"}, {label:"Brother"},{label:"Cousin"}]
};
var transform = {tag:'span',html:'.label'};
$('#list').json2html(json.List, transform);
</script>
what about: 关于什么:
var myVar = myJ.parseJSON();
$.each(myVar.List, function(key, value) {
items.push('<li>' + value + '</li>');
});
$('#myList').append( items.join('') );//myList is my unordered list's id.
)};
?? ??
As descriped In my comment jQuery.each has the arguments key
and value
. 如我的评论中所述,jQuery.each具有参数
key
和value
。 the variable this
is equal to the value ( arguments[1]
). 变量
this
是等于值( arguments[1]
Same as jQuery("selector").each has the arguments key
and element
where the variable this
is equal to the element
( arguments[1]
). 相同的jQuery(“选择”)。每个具有参数
key
和element
,其中所述可变this
等于element
( arguments[1]
OKAY: 好的:
var myVar = myJ.parseJSON();
also has to be replaced with: 还必须替换为:
var myVar = jQuery.parseJSON(myJ);
sorry for not seeing it :) 抱歉,没有看到它:)
You want to loop through the List
array that's on your object. 您想遍历对象上的
List
数组。
var myVar = myJ.parseJSON();
$.each(myVar.List, function(index, el) {
items.push('<li>' + el + '</li>');
});
You can just add it straight away, something like: 您可以直接将其添加,例如:
var parent = $("#myList");
$.each(myJ.parseJSON().List, function() {
$("<li></li>").html(this).appendTo(parent);
});
Here's some HTML 这是一些HTML
<pre></pre>
Then this bit of javascript 然后这段JavaScript
$('pre').text( JSON.stringify(mJ, null, "\t") );
It will render your JSON just like this 它将像这样呈现您的JSON
{
"fID": "00202020243123",
"name": "John Doe",
"List": [
"Father",
"Brother",
"Cousin"
]
}
Finally, you can use something like jQuery Snippet to make it look pretty 最后,您可以使用类似jQuery Snippet的外观使其漂亮
$('pre').snippet('javascript');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.