简体   繁体   English

从 JSON 中获取键名和值名并在 Highcharts 中使用

[英]Get Key names and Values names from JSON and use in Highcharts

I have a JSON file with the following structure:我有一个具有以下结构的 JSON 文件:

JSON Structure: JSON 结构:

[
    {
        "id": 1536700,
        "title": "final_output",
        "error": "",
        "data": [
            {
                "metric": 4940616.0,
                "title": "d_revenue"
            },
            {
                "metric": 5132162.0,
                "title": "p_revenue"
            },
            {
                "metric": 4954576.0,
                "title": "s_revenue"
            },
            {
                "metric": 4882217.0,
                "title": "u_revenue"
            },
            {
                "metric": 4869609.0,
                "title": "t_revenue"
            },
            {
                "metric": 5075422.0,
                "title": "w_revenue"
            },
            {
                "metric": 4461996.0,
                "title": "v_revenue"
            }
        ]
    }
]

Next Structure:下一个结构:

[
    {
        "run_id": 1536700,
        "code_title": "select_data",
        "error": "",
        "data": [
            {
                "user_name": "C_51",
                "num1": 51,
                "num2": 101,
                "num3": 151
            },
            {
                "user_name": "H_51",
                "num1": 51,
                "num2": 101,
                "num3": 151
            },
            {
                "user_name": "C_52",
                "num1": 52,
                "num2": 102,
                "num3": 152
            },
            {
                "user_name": "H_52",
                "num1": 52,
                "num2": 102,
                "num3": 152
            },
            {
                "user_name": "C_53",
                "num1": 53,
                "num2": 103,
                "num3": 153
            }
        ]
    }
]

I want to get the keys inside data and then decide what the x-axis would be and what the y-axis would be and I wanted to do something like http://jsfiddle.net/k32a59vL/1/ .我想获取data的键,然后决定 x 轴是什么以及 y 轴是什么,我想做类似http://jsfiddle.net/k32a59vL/1/ 的事情。

THis is my code:这是我的代码:

    $.getJSON(api, function(elem) {
        console.log(elem);
        elem.forEach(d => {

        });
    });

The console.log(elem) gives me the json structure mentioned above. console.log(elem)给了我上面提到的 json 结构。 How do I just get metric and title from the first structure and user_name, num1, num2, num3 .我如何从第一个结构和user_name, num1, num2, num3获取metric and title How do I get those values?我如何获得这些值?

Use Object.keys(yourObject) to get the keys使用 Object.keys(yourObject) 获取密钥

 $.getJSON(api, function(elem) {
      let keys=elem.map( structure =>  Object.keys(structure.data[0]))         
   });

Run the following snippet to check if it works运行以下代码段以检查它是否有效

 let ele= [{"id": 1536700,"title": "final_output","error": "", "data": [{"metric": 4940616.0,"title": "d_revenue"},{"metric": 5132162.0,"title": "p_revenue"},{"metric":4954576.0,"title": "s_revenue"},{"metric": 4882217.0,"title":"u_revenue"},{"metric": 4869609.0,"title":"t_revenue"},{"metric": 5075422.0,"title": "w_revenue"},{"metric": 4461996.0,"title": "v_revenue"} ] }, {"run_id": 1536700,"code_title": "select_data","error": "", "data": [{"user_name": "C_51","num1": 51,"num2": 101,"num3": 151},{"user_name": "H_51","num1": 51,"num2": 101, "num3": 151},{"user_name": "C_52","num1": 52,"num2": 102,"num3": 152},{"user_name": "H_52","num1": 52,"num2": 102,"num3":152},{"user_name": "C_53","num1": 53,"num2": 103,"num3": 153} ]}] console.log(ele.map( structure => Object.keys(structure.data[0])))
 .as-console-wrapper { max-height: 100% !important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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