簡體   English   中英

從json解析數組的java腳本

[英]java script to parse an array from json

{
    "took": 72,
    "hits": {
        "total": {
            "value": 10000
        }
    },
    "aggregations": {
        "2": {
            "buckets": [{
                "key": "Perf",
                "doc_count": 159874
            }]
        }
    }
}

有人可以指導我獲取桶的價值嗎

內置JSON應該為您完成繁重的工作:

const str = '{ "took": 72, "hits": { "total": { "value": 10000 } }, "aggregations": { "2": { "buckets": [{ "key": "Perf", "doc_count": 159874 }] } } }';
const obj = JSON.parse(str);
the_arry = obj['aggregations']['2']['buckets'];

這應該有效:

var myrs = { "took": 72, "hits": { "total": { "value": 10000 } }, "aggregations": { "2": { "buckets": [{ "key": "Perf", "doc_count": 159874 }] } } };
for(var i in myrs["aggregations"]) {console.log(myrs["aggregations"][i]['buckets']);}

您可以首先使用var obj = JSON.parse()解析 JSON 字符串。 它為您提供了要處理的字符串的對象表示。 將對象記錄到控制台以輕松發現結構體console.log(obj); 或使用在線 JSON 查看器來執行此操作。 假設結構是固定的, buckets數組可以通過var buckets = obj.aggregations["2"].buckets 注意屬性2的訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM