所以基本上我正在调用MVC控制器并以JSON格式返回查询结果。
<script>
$("#APINumber").focusout(function () {
$.getJSON('@Url.Action("CheckAPI", "WellInfo")', { APINumber: $(this).val() }, function (data) {
if (!data.isDuplicate) {
toastr.error('API Number does not exist in the database. Please check your API Number.', 'API Number Error')
$("#APINumber").val("")
}
var id = JSON.stringify(data.list);
$.each(data.list, function (value) {
viewModel.LeaseName(JSON.stringify(data.list[value]));
});
});
});
我想要做的是从我的查询中提取名为“LeaseName”的值。我创建$ .each循环来尝试解析它们,但我的文本框中填充了 viewModel.LeaseName(JSON.stringify(data.list [value])); 显示整个JSON字符串!见下文,这就是我得到的。
{"WellKey":221622,"OperatorKey":21,"LeaseKey":519,"APINumber":"03048374","FormattedAPINumber":"030-48374","RedrillCancelFlag":" ","WellDesignation":"518G1-34","FieldCode":"052","FieldAreaCode":"05200","FieldName":"Belridge, South","AreaCode":"00","AreaName":"Any Area","DistrictNumber":"4","CACountyCode":"030","CountyName":"Kern","LeaseName":"","WellNumber":"518G1-34","IsActive":1,"WellTypeCodeList":"","WellStatusCode":"A","WellStatusDate":"/Date(1342814313397)/","BLMInterestCode":"N","OperatorName":"Aera Energy LLC","OperatorCode":"A0610","IsCurrentActive":true,"Section":34,"Township":"28S","Range":"21E","BaseMeridian":"MD","IsBLMInterest":false,"IsEPAWell":false,"BaseOfFreshWater":"","AssignedEngineerID":-1,"IsHydraulicallyFractured":true,"AbandonedStandardStatusCode":"Not Abandoned","DirectionalStatusCode":"Directionally drilled","LocationDescription":"Fr SW cor 582 N 405 E","Elevation":"592 KB","IsDryHole":false,"Version":4,"Well2Version":3}
我不希望在我的文本框中显示整个内容,我只想从该JSON数据中显示一个SINGLE值。
任何帮助都会很棒!!!!
修改
尝试使用此
$.each(data.list, function (value) {
var json = JSON.stringify(data.list[value])
console.log(json["WellKey"]);
});
我在控制台中未定义...
编辑2
解决!不需要循环,我想使用$ .getJSON已经将其字符串化,我需要做的就是使用像data.list [0] .FieldName
之类的东西来访问我的数据。谢谢大家!
答案 0 :(得分:1)
从您所写的内容来看,似乎您的字符串太多了。假设
{"WellKey":221622,"OperatorKey":21,"LeaseKey":519,...}
是
的值data.list[value]
然后简单地做
viewModel.LeaseName(data.list[value].LeaseName);
答案 1 :(得分:1)
你只需要:
json["LeaseName"]
见jsFiddle:
但请注意,LeaseName在您返回的json中的值为 no 。
<强>更新强>
查看您的代码我相信这会输出您的价值:
data["LeaseName"]
当您在getJson
电话中时,data
变量已经是json。
如果您看一下这个小提琴,它只会以WellKey
的相同方式输出一个值,因为它有一个值: