[英]How to read data from json format using jquery
如何使用jquery从json格式读取数据。 以下是我尝试过的方法,但是很遗憾,我无法读取所需的确切数据。
$.ajax({
url: '@Url.HttpRouteUrl("GetDistrictList", new { })',
type: 'GET',
datatype: "json",
success: function (data, txtStatus, xhr) {
console.log(data);
if (data != null) {
$.each(data, function (i, item) {
alert(data[0]);
alert(data[0].DistrictCode)
alert(item);
alert(item[0]);
alert(item.DistrictCode);
$('#tblDistricts > tbody').append('<tr><td>'+item.DistrictCode+'</td><td>test</td><td>test</td><td>test</td><td>test</td><td>test</td></tr>');
})
}
},
error: function (xhr, textStatus, errorThrown) {
console.log("error in GetDistrictList : " + errorThrown);
}
});'
在警告框中,我仅获得“未定义”或“ [对象] [对象],我无法读取确切的数据。 我被困在这里。
编辑:Web api将返回数据作为列表对象。
[HttpGet]
[Route("GetDistrict/", Name = "GetDistrictList")]
public List<DistrictModels> GetDistrictList()
{
BAL_District oBAL_District = new BAL_District();
return oBAL_District.GetDistrictList();
}
使用var Data = $.parseJSON(response);
例
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );
JSON.stringify
将Javascript对象转换为JSON文本,并将该JSON文本存储在字符串中。
JSON.parse
将JSON文本字符串转换为Javascript对象。
仅供参考,自jQuery 3.0起,不推荐使用$ .parseJSON。 要解析JSON对象,请改用本机JSON.parse方法。
var json = [ { 'red': '#f00' }, { 'green': '#0f0' }, { 'blue': '#00f' } ]; $.each(json, function () { $.each(this, function (name, value) { console.log(name + '=' + value); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我认为您希望调用此函数-http://api.jquery.com/jquery.parsejson/
如果获取[object,object],则意味着您已经获取了Json Object。 如果您需要一些说明,请分享您想要的期望Json。
使用JSON.stringify(object)
。 这是打印的超级安全方式。
$.ajax({
url: '@Url.HttpRouteUrl("GetDistrictList", new { })',
type: 'GET',
datatype: "json",
success: function (data, txtStatus, xhr) {
console.log(data);
if (!!data) {
// '!data' means data variable is not available and '!!data' returns true:boolean if variable is valid
//idx is 'index', I made the code as a loop. It will not work if array is empty.
//It is good way to avoid error
for(var idx in data){
if(idx == 0) {
//JSON.stringify makes object type to string type. It is safe to use since it is a native javascript function
alert(JSON.stringify(data[idx]);
//You can access member by 'data[idx].memberName'
//DOM adding code can be here
}
}
}
},
error: function (xhr, textStatus, errorThrown) {
console.log("error in GetDistrictList : " + errorThrown);
}
});
假设服务器端脚本未设置正确的Content-Type: application/json
响应标头,则需要使用dataType: 'json'
参数向jQuery指示这是JSON。
您不应在$.each()
使用data
变量。 然后,您可以使用$.each()
函数遍历数据:
$.ajax({
url: '@Url.HttpRouteUrl("GetDistrictList", new { })',
type: 'GET',
datatype: "json",
success: function (data, txtStatus, xhr) {
console.log(data);
if (data != null) {
$.each(data, function (index, element) {
alert(element);
alert(element[0]);
alert(element.DistrictCode);
$('#tblDistricts > tbody').append('<tr><td>'+element.DistrictCode+'</td><td>test</td><td>test</td><td>test</td><td>test</td><td>test</td></tr>');
});
}
},
error: function (xhr, textStatus, errorThrown) {
console.log("error in GetDistrictList : " + errorThrown);
}
});'
或使用$.getJSON
方法:例如:
$.getJSON('/functions.php', { get_param: 'value' }, function(data) {
$.each(data, function(index, element) {
$('body').append($('<div>', {
text: element.name
}));
});
});
注意:从jQuery 3.0开始,不推荐使用$.parseJSON
。 要解析JSON对象,请改用本机JSON.parse
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.