2016-07-02 52 views
-1

我有以下的有效JSON(我測試了jsonlint.com):如何解析簡單的JSON我JAVA

{ 
"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

在Java(與GSON)它是如此簡單:

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

您可以提供自己的類如第二個參數,在這種情況下,它將被反序列化到該類的新實例中。

+0

這和Jackson很相似,除了它是「readValue」和「ObjectMapper」 –