为了账号安全,请及时绑定邮箱和手机立即绑定

javascript从json中的嵌套数组打印值

javascript从json中的嵌套数组打印值

子衿沉夜 2023-09-07 16:22:58
有人可以帮我从下面的 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);
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

像这样

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);


查看完整回答
反对 回复 2023-09-07
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信