如何从DataSet中提取JSON格式的DataTable
我正在使用Newtonsoft DLL进行序列化。 我目前正在以json格式从webmethod返回DataTable。 它工作正常。 但我希望通过返回DataSet而不是DataTable来做同样的事情。 我尝试过很多东西,但似乎都没有。
function GetDropDownData() { var myDropDownList = $('.myDropDownLisTId'); $.ajax({ type: "POST", url: "test.aspx/GetDropDownDataWM", data: '{name: "abc" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { $.each(jQuery.parseJSON(data.d), function () { myDropDownList.append($("").val(this['id']).html(this['name'])); }); }, failure: function (response) { alert(response.d); } }); } function OnSuccess(response) { console.log(response.d); alert(response.d); }
返回Json DataTable:
[{"name":"sam","id":"1"},{"name":"mark","id":"2"}]
Json DataSet返回:
{"patients":[{"name":"sam","id":"1"},{"name":"mark","id":"2"}],"medications":[{"id":"1","medication":"atenolol"},{"id":"2","medication":"amoxicillin"}]}
小提琴 演示
var json=jsonds['patients'];
with var json=data['patients'];
最后,我成功了。
在Ajax
Success内部,请执行以下操作…
var dataSet = jQuery.parseJSON(data.d); var dataTable = dataSet["patients"]; $.each(dataTable, function () { myDropDownList.append($("").val(this['id']).html(this['name'])); });
var aa={"patients":[{"name":"sam","id":"1"},{"name":"mark","id":"2"}],"medications":[{"id":"1","medication":"atenolol"},{"id":"2","medication":"amoxicillin"}]} console.log(aa["patients"]); console.log(aa["medications"]);
这只是一个例子
您可以看到,当您有两个级别的json对象时,您必须将其与名称一起使用
你的对象就像数据表一样
请在这里查看演示Demo