javascript - javascript从json中的嵌套数组中打印一个值
问题描述
有人可以帮我从下面的 json 解析 action.keyword 的值。我只能解析到“命中”。
const rsp = `{ "took": 10, "hits": { "total": { "value": 520 } }, "aggregations": { "2": { "buckets": [{ "1": { "hits": { "total": { "value": 2 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "Start" ] }, "key": "TEST2", "doc_count": 4566 }] } } }, { "1": { "hits": { "total": { "value": 10 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "End" ] }, "key": "TEST3", "doc_count": 454 }] } } } ] } } }`
const obj = JSON.parse(rsp);
const recEvents = obj['aggregations']['2']['buckets'];
const act_key = recEvents['1']['hits']['hits'];
console.log(act_key);
解决方案
像这样
const act_key = recEvents[0]['1']['hits']['hits'][0]['fields']['action.keyword'];
const rsp = `{ "took": 10, "hits": { "total": { "value": 520 } }, "aggregations": { "2": { "buckets": [{ "1": { "hits": { "total": { "value": 2 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "Start" ] }, "key": "TEST2", "doc_count": 4566 }] } } }, { "1": { "hits": { "total": { "value": 10 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "End" ] }, "key": "TEST3", "doc_count": 454 }] } } } ] } } }`
const obj = JSON.parse(rsp);
const recEvents = obj['aggregations']['2']['buckets'];
const act_key = recEvents[0]['1']['hits']['hits'][0]['fields']['action.keyword'];
console.log(act_key);
推荐阅读
- r - 如何使用 R 中的向量逐列过滤数据帧?
- python - 分组并将其分配给python pandas中的中间组
- javascript - 使用 Multer 解析多部分 JSON
- python - 有没有办法在多维张量上获得 torch.mode
- python - R 中 qexp 的 Python 等价物是什么?
- r - 如何在 R 中使用 ggplot 绘制多个条形图
- macos - VSCode 远程资源管理器不显示远程容器
- r - ggplot 用于具有不同颜色的负值的直方图
- go - 缺少数据 Hyperledger Fabric 链码
- javascript - 可以忽略牛队的 process 方法返回的 promise 吗?