如何解析简单的JSON i JAVA

时间:2016-07-02 12:21:27

标签: java json

我有以下有效的JSON(我在jsonlint.com上测试过):

{
"Structure": {
    "paper": "Paper",
    "solid_reviewer": "Solid Reviewer",
    "second_reviewer": "2. Reviewer",
    "third_reviewer": "3. Reviewer"
},
"ReviewerGroup": {
    "3457": {
        "paper": "3457",
        "solid_reviewer": {
            "druidkey": "168",
            "match": "Interest match"
        },
        "second_reviewer": {
            "druidkey": "192",
            "match": "Interest match"
        },
        "third_reviewer": {
            "druidkey": "155",
            "match": "Interest match"
        }
    },
    "3458": {
        "paper": "3458",
        "solid_reviewer": {
            "druidkey": "229",
            "match": "Interest match"
        },
        "second_reviewer": {
            "druidkey": "145",
            "match": "Interest match"
        },
        "third_reviewer": {
            "druidkey": "123",
            "match": "Interest match"
        }
    },
    "275": {
        "paper": "275",
        "solid_reviewer": {
            "druidkey": "999",
            "match": "Interest match"
        },
        "second_reviewer": {
            "druidkey": "412",
            "match": "Interest match"
        },
        "third_reviewer": {
            "druidkey": "713",
            "match": "Interest match"
        }
    }
},
"failedPapers": {}
 }

在PHP中,要解码这个我只需:

$response = json_decode($response);

我可以遍历$ response对象:

foreach ($response->ReviewerGroup as $r){
  $paperRev1 = $paperReviewDAO->find(**$r->paper**, 1);
  $revConf = $revConfDAO->findRevConfFromReviewer($confId,              
     **$r->solid_reviewer->druidkey**);
.
.
}

但在Java中似乎要复杂得多。

我已经看过很多关于如何读取一个对象或一个对象数组的例子。

但是我如何获得所有'ReviewerGroup'(RG)对象并迭代它们?我需要从每个中提取信息并将数据插入到数据库中(我知道如何做到这一点......)作为每个RG对象的实体。

我不在乎使用什么JSON库。我看过GSON和杰克逊。

感谢您的任何建议和想法...

/ 金

1 个答案:

答案 0 :(得分:-1)

在Java(与Gson)中,它很简单:

Map<String, Object> result = new Gson().fromJson(response, Map.class);

您可以提供自己的类作为第二个参数,在这种情况下,它将被反序列化为该类的新实例。