使用 GSON 解析长 JSON

Parsing a long JSON using GSON

我正在尝试像这样解析很长的 JSON。

{
  "BlastOutput2": [
    {
      "report": {
        "program": "blastn",
        "version": "BLASTN 2.6.0+",
        "reference": "Stephen F. Altschul, Thomas L. Madden, Alejandro A. Schäffer, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), \"Gapped BLAST and PSI-BLAST: a new generation of protein database search programs\", Nucleic Acids Res. 25:3389-3402.",
        "search_target": {
          "subjects": [
            "Subject_1"
          ]
        },
        "params": {
          "expect": 0.0001,
          "sc_match": 1,
          "sc_mismatch": -2,
          "gap_open": 5,
          "gap_extend": 2,
          "filter": "F"
        },
        "results": {
          "bl2seq": [
            {
              "query_id": "Query_1",
              "query_title": "F1-125",
              "query_len": 806,
              "hits": [
                {
                  "num": 1,
                  "description": [
                    {
                      "id": "gi|251831106|ref|NC_012920.1|",
                      "title": "gi|251831106|ref|NC_012920.1| Homo sapiens mitochondrion, complete genome"
                    }
                  ],
                  "len": 16569,
                  "hsps": [
                    {
                      "num": 1,
                      "bit_score": 619.793,
                      "score": 322,
                      "evalue": 3.47859e-180,
                      "identity": 458,
                      "query_from": 57,
                      "query_to": 572,
                      "query_strand": "Plus",
                      "hit_from": 16060,
                      "hit_to": 16569,
                      "hit_strand": "Plus",
                      "align_len": 516,
                      "gaps": 6,
                      "qseq": "GTATTGACTCACCGCTTAACAACGGCAATGTATTTCGTACATTACTGCCAGCCACAAAGAAAGTTGTACAGTACCATAAATACTTGACTACCTGTAGTACATAAAAACTCAACCCACATCAAAACCCCAAAAGACGAGACAGAAAAGCAAGTTTTTCAATCAACCTTCAACTGTCACACATCAACTGCAACTCCAAAGCCACCCCTCACCCACTAGGATATCAACAAACCTACCCGCCCTTAACAGTACATAGCACATAAAGTCATTTACCGTACATAGCACATTACAGTCAAATCCCTTCTCGTCCCCATGGATGACCCCCCTTTTTTTTGTGGTCCCCTGACCACCATCCTCCGTGTTTTTTTTTTTTCTCGCACAAGAGTGCTACTCTCCTCGTTCTGGGCCCATAACACTTGGGGGTAGCTAAAGTGAACTGTATCCGACATCTGGTTCCTACTTCAGGGTCATAAAGCCTAAATAGCCCACACGTTCCCCTTTTTATAAGACATCACGATG",
                      "hseq": "GTATTGACTCACCCATCAACAACCGCTATGTATTTCGTACATTACTGCCAGCCACCATGAATATTGTACGGTACCATAAATACTTGACCACCTGTAGTACATAAAAACCCAATCCACATCAAAACCCCCTCC--CCATGCTTACAAGCAAGTACAGCAATCAACCCTCAACTATCACACATCAACTGCAACTCCAAAGCCACCCCTCACCCACTAGGATACCAACAAACCTACCCACCCTTAACAGTACATAGTACATAAAGCCATTTACCGTACATAGCACATTACAGTCAAATCCCTTCTCGTCCCCATGGATGACCCCCCTCAGATAGG-GGTCCCTTGACCACCATCCTCCGTG--AAATCAATATCCCGCACAAGAGTGCTACTCTCCTCGCTCCGGGCCCATAACACTTGGGGGTAGCTAAAGTGAACTGTATCCGACATCTGGTTCCTACTTCAGGGTCATAAAGCCTAAATAGCCCACACGTTCCCC-TTAAATAAGACATCACGATG",
                      "midline": "|||||||||||||  | |||||| || |||||||||||||||||||||||||||| | |||  |||||| |||||||||||||||||| ||||||||||||||||||| ||| |||||||||||||||      | |  |  | ||||||||    ||||||||| |||||| ||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||| ||||||||||||||||| |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||    |  | |||||| ||||||||||||||||||     |   | || |||||||||||||||||||||||| || ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||  ||||||||||||||||"
                    },
                    {
                      "num": 2,
                      "bit_score": 108.358,
                      "score": 56,
                      "evalue": 3.15269e-026,
                      "identity": 88,
                      "query_from": 573,
                      "query_to": 666,
                      "query_strand": "Plus",
                      "hit_from": 1,
                      "hit_to": 89,
                      "hit_strand": "Plus",
                      "align_len": 94,
                      "gaps": 5,
                      "qseq": "GATCACAGGTCTATCACCCTATTAAACCACTCACGGGGAGCTCTCCATGCATTTGGTATTTTTCGTCTGGGGGGGGTATGCACGCGATAACATT",
                      "hseq": "GATCACAGGTCTATCACCCTATTAA-CCACTCACGGG-AGCTCTCCATGCATTTGGTA-TTTTCGTCT--GGGGGGTATGCACGCGATAGCATT",
                      "midline": "||||||||||||||||||||||||| ||||||||||| |||||||||||||||||||| |||||||||  ||||||||||||||||||| ||||"
                    }
                  ]
                }
              ],
              "stat": {
                "hsp_len": 14,
                "eff_space": 13111560,
                "kappa": 0.620991117264206,
                "lambda": 1.33270576282382,
                "entropy": 1.12409184650114
              }
            }
          ]
        }
      }
    }
  ]
}

我正在使用 GSON,通过查看一些示例,有些人使用 Class 将所有内容定义到 JSON 文件中,但我不知道这是否是最好的方法这个特殊情况,因为我只想要 JSON 的结果部分,有人可以帮我解决这个问题吗?

您不需要创建一堆 class 来解析复杂的 JSON 字符串。让我们将它解析为 HashMap 然后遍历它以获得你想要的。

Type type = new TypeToken<HashMap<String, Object>>(){}.getType();
HashMap<String, Object> myMap = gson.fromJson("your_json_string", type);

这样,你就有了一个多层次的HashMap,让我们用递归的方法遍历myMap得到你想要的结果。

示例代码:

public static void main(String[] args) throws ParseException, JsonSyntaxException, IOException {
    Type type = new TypeToken<HashMap<String, Object>>() {
    }.getType();
    Gson gson = new Gson();
    HashMap<String, Object> map = gson.fromJson("your_json_string", type);
    for (String key : map.keySet()) {
        Object obj = map.get(key);
        if (obj instanceof List) {
            for (Object o : (List) obj) {
                if (o instanceof Map) {
                    loop((Map) o);
                } else {
                    System.out.println(key + " : " + o);
                }
            }
        } else if (obj instanceof Map) {
            loop((Map) obj);
        } else {
            System.out.println(key + " : " + obj);
        }
    }
}

private static void loop(Map<String, Object> map) {
    for (String key : map.keySet()) {
        Object obj = map.get(key);
        if (obj instanceof List) {
            for (Object o : (List) obj) {
                if (o instanceof Map) {
                    loop((Map) o);
                } else {
                    System.out.println(key + " : " + o);
                }
            }
        } else if (obj instanceof Map) {
            loop((Map) obj);
        } else {
            System.out.println(key + " : " + obj);
        }
    }
}