jQuery-将JSON转换为< table> [英] jquery - JSON to <table>
本文介绍了jQuery-将JSON转换为< table>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下形式的Json
I have Json of the form
[{"id":39,"data":1},{"id":40,"data":2}]
使用<$ c无法正确解析$ c> jQuery.parseJSON()
我需要获取此数据并创建html 表
。我当时正在考虑在js中动态创建HTML。
I need to take this data and create a html table
. I was thinking of creating the HTML dynamically in the js.
A。我怎么解析json?
B.动态html是最佳途径吗?
A. How can I parse the json?
B. Is dynamic html the best route?
更新
对不起。显然我的问题不清楚。这是jQuery
Update
I apologize. Evidently my question is not clear. Here is the jquery
$.get('Service.aspx',
{ p1: value, p2: value },
function (data) {
notesJson = data;
alert(notesJson);//Json comes back as I said here...
var noteSet = jQuery.parseJSON(notesJson);
alert(noteSet.notes);
});
笔记在Json中确实存在。
第二个警报未定义失败。
notes does exist in the Json. The second alert fails "undefined".
推荐答案
基于您对问题的评论,确定:
Ok based on you comment on your question:
使用 $。getJSON
代替 $。get
:
$.getJSON('someurl', {/*somedata*/}, function(json_data){
//no need for parsejson
//use the json_data object
var table_obj = $('table');
$.each(json_data, function(index, item){
var table_row = $('<tr>', {id: item.id});
var table_cell = $('<td>', {html: item.data});
table_row.append(table_cell);
table_obj.append(table_row);
})
})
这篇关于jQuery-将JSON转换为< table>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文