[英]How to access json key value from ajax success?
I want to access the logs of the data I am getting from the ajax response.我想访问我从 ajax 响应中获得的数据的日志。 I want to create it as an array so that I can access all the logs.我想将它创建为一个数组,以便我可以访问所有日志。 But right now whenever I parse the data, it takes it as a single object value.但是现在每当我解析数据时,它都会将其作为单个对象值。
In the data there is only one _id=1.数据中只有一个_id=1。 I want to access the logs.我想访问日志。
[
{
"_id": "1",
"qos": "Access",
"date": "2020-02-26",
"logs": [
{
"qos": "Access",
"date": "2020-02-26",
"hour": 23,
"cellName": "A11"
},
{
"qos": "Access",
"date": "2020-02-26",
"hour": 21,
"cellName": "A12"
}
{
similar for entries
},{}
}
]
$.ajax({
type: 'POST',
async: 'false',
url: '/data_from_datastore',
contentType: 'application/json',
data: JSON.stringify(sendDate1),
success: function (data) {
debugger;
data = JSON.parse(data); // Here I want to create an array of all the log values
I want to read the data as:我想将数据读取为:
[
{
"qos": "Access",
"date": "2020-02-26",
"hour": 23,
"cellName": "A11"
},
{
"qos": "Access",
"date": "2020-02-26",
"hour": 21,
"cellName": "A12"
}
{
similar for entries
}
}
]
Is there any way to parse only the log part?有没有办法只解析日志部分?
I don't think you can parse only the piece of data.我认为你不能只解析那段数据。 But you can use array.reduce, like但是你可以使用array.reduce,比如
const logs = data.reduce((result, item) => { return [...result, item.logs] }, []);
Or some not ECMA2015 version, like或者一些不是 ECMA2015 版本,比如
var logs = [];
data.forEach(function(item){ logs.push(item.logs); });
Or if you need only the first item of the data array -或者,如果您只需要数据数组的第一项 -
var logs = JSON.parse(data)[0].logs;
尝试
result = JSON.parse(data)[0].logs;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.