简体   繁体   English

JSON解析数组中的数组(JAVA)

[英]JSON parsing arrays from arrays (JAVA)

I have a String which contains JSON File information. 我有一个包含JSON文件信息的字符串。

    static String JSON_STRING2 ="{\"lex\": {\"seg\": [ [ 0, 8 ], [ 9, 5 ], [ 15, 6 ] ], \"s\": [ [ 0, 21 ] ], \"p\": [ [ 0, 21 ] ]}, \"morphology\": { \"msd\": [ [ [ \"red\", \"Agpmsnn\" ], [ \"red\", \"Agpfpan\" ], [ \"red\", \"Agpmsvn\" ] ], [ [ \"flower\", \"Ncfsgn-\" ], [ \"flower\", \"Ncfpnn-\" ], [ \"flower\", \"Ncfpvn-\" ] ], [ [ \"blossom\", \"Ncmsnn-\" ] ] ], \"stem\": [ \"rr\", \"ff\", \"bb\" ]}}";

What i want to do is to take this data ant save into arrays for further string comparision tasks, but at the moment i do not know how to reach bottom layer of "seg" layer and be able to take for example [0,8] values by its position in array. 我想做的是将这个数据蚂蚁保存到数组中以进行进一步的字符串比较任务,但是目前我不知道如何到达“ seg”层的底层,并且能够以例如[0,8]值根据其在数组中的位置。 As far as I get is taking whole Array element via get() . 据我所知是通过get()获取整个Array元素。 I tried looping data but most of the time i end up with wrong array or arrayList types. 我尝试循环数据,但大多数情况下,我最终遇到错误的array或arrayList类型。

My preview code: 我的预览代码:

public static void main(String[] args) {

    // Create root JSON Object
    JSONObject object = new JSONObject(JSON_STRING2);
    System.out.println("--------------------------------");

    // Get Data Values
    JSONObject LEX = object.getJSONObject("lex");

    System.out.println("------------- Data Values ---------------");

    System.out.println("Array Words : " + LEX.getJSONArray("seg").get(1));
    System.out.println("Array Sentences : " + LEX.getJSONArray("s"));
    System.out.println("Array Paragraphs : " + LEX.getJSONArray("p"));
    System.out.println("--------------------------------");

    // Get Data Values
    JSONObject MORP = object.getJSONObject("morphology");
    System.out.println("------------- Data Values ---------------");
    System.out.println("Array wordCharacteristic : " + MORP.getJSONArray("msd"));}

Any tips or tricks? 有技巧或窍门吗? I have read bounch of articles here non of them parsing familiar data to mine. 我在这里读过很多文章,没有一篇文章分析熟悉的数据。 Thanks. 谢谢。

First of all, note that JSONArray are 0-indexed, like the arrays and lists in java. 首先,请注意, JSONArray的索引为0,就像Java中的数组和列表一样。 So LEX.getJSONArray("seg").get(1) return the second item in seg , thus printing [9,5] . 因此, LEX.getJSONArray("seg").get(1)返回seg第二项,从而输出[9,5]

If you want the first , just use LEX.getJSONArray("seg").get(0) . 如果要使用第一个 ,只需使用LEX.getJSONArray("seg").get(0)

Now you can iterate on those JSONArray. 现在,您可以在这些JSONArray上进行迭代。 There many ways to do that, here is one possibility: 有很多方法可以做到这一点,这是一种可能:

    for (Object seg : LEX.getJSONArray("seg")) {
        JSONArray values = (JSONArray) seg;
        for (Object objValue : values) {
            int value = (int) objValue;
            System.out.print(value + " ");
        }
        System.out.println();
    }

will print this out: 将打印出来:

0 8
9 5
15 6

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

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