简体   繁体   中英

How can I build a graph from specific key,value pairs in a JSON file?

I have some JSON that resembles this:

{ 
    "Variable": "Id",
    "Stat": 250,
    "Value": 2,
    "Data": {
        "Key1_std": 20,
        "Key1_25%": 100,
        "Key1_count": 14,
        "Key1_75%": 13,
        "Key1_mean": 10,
        "Key2_std": 20,
        "Key2_25%": 100,
        "Key2_count": 14,
        "Key2_75%": 13,
        "Key2_mean": 10,
        "Key3_std": 20,
        "Key3_25%": 100,
        "Key3_count": 14,
        "Key3_75%": 13,
        "Key3_mean": 10,
        },
    "Omega": 0.1
}

I need to create a graph that displays only the values for the "*_mean" keys.

How should I go about fetching these specific values from the JSON?

They are randomly spaced throughout the real file. Since JSON is not a regular language, I've avoided regex ... lest my computer be p͏͔͚̣o͚̤͙̟̟̖ͅss̷̱̣̩̞̟͙e͉̘̩͟s̩͖̹͍s̯͓͍̱͠e̩d̡̯̯̦̣̱ͅͅ b̠̙̗͓y͓̹̳̩̫͎̳͢ ͞C̢͇̹t͎͇h̻͇͜ͅu̻̭͜l͈̝̫u̢̩̹͎̭̫.

Thanks in advance.

You can iterate over keys with object.keys.

Code is like this:

var keys = object.keys(json.Data);

var finalArr = [];

for(i=0;i<keys.length;i++){
  if(keys[i].indexOf('mean') > -1){ // mean is part of string
     finalArr.push(json.Data[keys[i]]);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM