javascript 解析 JSON 对象以获取列数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27394564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:25:48  来源:igfitidea点击:

Parsing a JSON object to get column data

javascriptjson

提问by AndroidNoobie

Say I have data in a JSON object formatted similar to...

假设我有一个 JSON 对象中的数据,其格式类似于...

{"data":
       [["X","Y","Z"], 
       ["52","23","10"],
       ["46","65","32"]]
}

So essentially, each row takes the form [X, Y, Z].

所以基本上,每一行都采用形式[X, Y, Z]

What would be the easiest way to then access an entire "column" or vector of data? For example, let's assume the "X", "Y", and "Z" is a header row, and I want to access all of the "X" data.

然后访问整个“列”或数据向量的最简单方法是什么?例如,假设“X”、“Y”和“Z”是标题行,我想访问所有“X”数据。

Do I need to iterate over the entire object somehow to retrieve the first member of each element, which would correspond to the "X" column?

我是否需要以某种方式遍历整个对象以检索每个元素的第一个成员,这将对应于“X”列?

I'd like to do this in JavaScript if possible.

如果可能,我想在 JavaScript 中执行此操作。

Thanks much for any assistance!

非常感谢您的帮助!

采纳答案by Braj

Try below sample code that is simple and straight forward to access the first column.

试试下面的示例代码,它简单直接地访问第一列。

First iterate the first row to find the correct column then get the values of that column for next rows.

首先迭代第一行以找到正确的列,然后为下一行获取该列的值。

<script type="text/javascript">
    var obj = {
        "data" : [ 
                   [ "X", "Y", "Z" ], 
                   [ "52", "23", "10" ],
                   [ "46", "65", "32" ] 
                 ]
    };
    var column;
    for (var i = 0, j = obj.data.length; i < j; i++) {
        if (obj.data[0][i] === 'X') {
            column = i;
            break;
        }
    }

    for (var i = 1, j = obj.data.length; i < j; i++) {
        var val = obj.data[i][column];
        console.log(val);
    }
</script>

回答by syllabus

"jsonpath" seems to be a good option too:

“jsonpath”似乎也是一个不错的选择:

that would lead to something like

这会导致类似的事情

vector X: /data[1][*][1]
vector Y: /data[1][*][2]
vector Z: /data[1][*][3]

回答by Pratik Butani

I have run this code for Android You may tried out in JavaScript with this type of logic:

我已经为 Android 运行了这段代码你可以在 JavaScript 中尝试使用这种类型的逻辑:

    String json = "{\"data\":[[\"X\",\"Y\",\"Z\"],[\"52\",\"23\",\"10\"],[\"46\",\"65\",\"32\"]]}";
    try {
        JSONObject jsonObj = new JSONObject(json);

        Log.i(TAG, "DatA : " + jsonObj.getJSONArray("data").length());
        JSONArray array = jsonObj.getJSONArray("data");
        for(int i=0; i<array.length(); i++) {
            Log.i(TAG, "Value : " + array.getJSONArray(i).getString(0));
        }

    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Output in Logcat:

Logcat 中的输出:

12-10 12:26:32.628: I/Demo(28709): DatA : 3
12-10 12:26:32.629: I/Demo(28709): Value : X
12-10 12:26:32.629: I/Demo(28709): Value : 52
12-10 12:26:32.629: I/Demo(28709): Value : 46

Edited: For JavaScript:

编辑:对于 JavaScript:

<html>
<body>
<p id="demo"></p>

<script>
var text = '{\"data\":[[\"X\",\"Y\",\"Z\"],[\"52\",\"23\",\"10\"],[\"46\",\"65\",\"32\"]]}';
var out = "";
obj = JSON.parse(text);

arr = obj.data;
console.log(arr);
for(i = 0; i < arr.length; i++) {
        out += arr[i][0] + "<br>";
}

document.getElementById("demo").innerHTML = out;

</script>

</body>
</html>

回答by blad

You should try using something like Gson or Hymanson Json Processor to handle the JSON and deserialize it into something you can use.

您应该尝试使用 Gson 或 Hymanson Json Processor 之类的东西来处理 JSON 并将其反序列化为您可以使用的东西。

One you do so, you will be able to do something like:

你这样做,你将能够做这样的事情:

JsonObject json = new JsonParser().parse("...json string...");
JsonArray data = json.get("data");
JsonArray labels = data.getAsJsonArray(0)
for (int i = 1; i < data.size(); i++) {
    JsonArray points = data.getAsJsonArray(i);
    // do something with:  points.getInt(0) -- x column
    // do something with:  points.getInt(1) -- y column
    // do something with:  points.getInt(2) -- z column
}