jquery – JSON到

我有Json的forms

[{"id":39,"data":1},{"id":40,"data":2}] 

它没有用jQuery.parseJSON()正确解析

我需要获取这些数据并创建一个html table 。 我正在考虑在js中动态创建HTML。

A.我如何解析json?
B.动态HTML是最好的路线吗?

更新
我道歉。 显然我的问题不明确。 这是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中确实存在注释。 第二个警报失败“未定义”。

好的,基于你对你的问题的评论:

使用$.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 = $('', {id: item.id}); var table_cell = $('', {html: item.data}); table_row.append(table_cell); table_obj.append(table_row); }) }) 

你拥有的是一个数组,你需要一个对象。 尝试

  { "notes" : [{"id":39,"data":1},{"id":40,"data":2}] } 

作为json

或者这样做

  alert(noteSet[0]); alert(noteSet[1]); 

你说你正在使用.NET,所以你可以使用

 return Json(yourObject, JsonRequestBehavior.AllowGet); 

而不是JavaScriptSerializer。 你不必解析它。

这是另一个很好的库来实现它: https : //github.com/afshinm/Json-to-HTML-Table

只需将JSON数据传递给此库并获取HTML表:

 //Only first parameter is required var jsonHtmlTable = ConvertJsonToTable(objectArray, 'jsonTable', null, 'Download'); 

试试这个完整代码:

     

一个接受JSON数据填充表的jquery插件可以做到。 jsonTable

这是另一种考虑典型数据库查询的表标题的方法。 根据尼尔的回答:

 $.getJSON('bower.json', {}, function(json_data) { var table = $('table'); $.each(json_data, function(key, item) { var table_row = $(''); $.each(item, function(itemKey, itemValue) { if (key == 0) { table.append($('', {html: itemKey})); } table_row.append($('', {html: itemValue})); }); table.append(table_row); }); });