如何从jQuery中的JSON获取对象

时间:2019-05-01 04:43:18

标签: jquery json

我正在从json中的jquery ajax(在响应数组中)获得以下响应

{
    "status": true,
    "message": "Data fetched successfully",
    "data": [{
        "id": "19",
        "employer_id": "5bea870168ebeb6db4d612c3",
        "employee_id": "5bea868b68ebeb6db4d612c1"
    }],
    "count": 1
}

如何从此json(而不是php)中的javascript / jquery中获取“计数”对象?

我尝试使用以下代码,但对我不起作用

alert(response.count);

1 个答案:

答案 0 :(得分:0)

您应该在成功功能中使用console.log(data),并且您的对象response.count仍然有效。

如果响应是数组,则可以使用console.log(response[0].count);

let response = {
    "status": true,
    "message": "Data fetched successfully",
    "data": [{
        "id": "19",
        "employer_id": "5bea870168ebeb6db4d612c3",
        "employee_id": "5bea868b68ebeb6db4d612c1"
    }],
    "count": 1
};
console.log(response.count);

response = [{
    "status": true,
    "message": "Data fetched successfully",
    "data": [{
        "id": "19",
        "employer_id": "5bea870168ebeb6db4d612c3",
        "employee_id": "5bea868b68ebeb6db4d612c1"
    }],
    "count": 1
}];

console.log(response[0].count);