WEB API 的结果 调用 Web API 后,我可以获得以下 JSON 格式(来自 Telerik Fiddler 的结果)。
[
{
"modelEmployeeID": 1,
"modelFirstName": "Mike",
"modelLastName": "Tai",
"modelBirthDate": "1900-01-01T00:00:00",
"modelPosition": "IT Exec",
"modelHireDate": "2017-01-01T00:00:00",
"modelStateID": 1
},
{
"modelEmployeeID": 2,
"modelFirstName": "Sue",
"modelLastName": "Yoong",
"modelBirthDate": "1990-07-16T00:00:00",
"modelPosition": "IT Manager",
"modelHireDate": "2017-02-28T00:00:00",
"modelStateID": 2
}
]
JQUERY
**And I am calling the API using JQuery with the following method:**
var dbImpl = {
load: function (loadOptions) {
return $.getJSON('http://localhost:777/employee/testjson');
},
};
预期结果 我希望结果具有多个对象数组:(我希望结果标记为“1”,当前结果标记为“2”)
最佳答案
您似乎希望 $.getJSON()
同步返回 JSON。它不是这样工作的,它是异步的(它只是 uses Ajax internally )。我绝对不确定我是否知道您真正想要什么,但您肯定会这样做吗?
var dbImpl;
$.getJSON('http://localhost:777/employee/testjson', function(result) {
dbImpl = result;
});
所做的就是使用 getJSON
检索 JSON,然后将其分配给 success 函数中的 dbImpl
。假设数据与您在问题中显示的数据相同,dbImpl
将类似于您问题中的 #1 屏幕截图中的 statestest
。
关于jquery - 使用 JQuery 将 JSON 对象解析为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227823/