亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號