簡體   English   中英

在Java NetBeans中解析JSON字符串

[英]Parsing JSON string in Java NetBeans

誰能告訴我如何從每個元標記中獲取og:descriptionog:titlebook:release_datebook:authorbook:isbn (請參閱下面的JSON內容)最終,這些值將顯示在表中,因此我需要將它們作為Java對象(字符串)。

到目前為止,我已經嘗試使用許多外部庫,但這是我使用JSON的第一個項目,因此我不了解它們的工作原理。

供您參考,我正在使用NetBeans,並正在提供一個寧靜的Web服務。

我設法使用以下代碼將它們轉換為JSON字符串:

    JSONObject obj = new JSONObject(builder.toString());
    String theJsonString = obj.toString();

但是當我嘗試這樣做時:

    ObjectMapper mapper = new ObjectMapper();
                  Map<String,Object> data = mapper.readValue(theJsonString.getBytes(), Map.class);
                  Map items = (Map) data.get("items");
                  Map pagemap = (Map) items.get("pagemap");
                  Map metatags = (Map) pagemap.get("metatags");
                  String b_title = (String) metatags.get("og:title");

     System.out.println(b_title);

我收到此錯誤:

java.lang.ClassCastException:無法將API.GCustSearchAPI.main(GCustSearchAPI.java:77)上的java.util.ArrayList強制轉換為java.util.Map

第77行是這樣的:

Map items = (Map) data.get("items");

這是json內容:

{
"items": [
{
"pagemap": {
"metatags": [
 {
  "og:description": "A boxed set, including the titles 'Harry Potter and the Philosopher's Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire', 'Harry Potter and the Order of the Phoenix', 'Harry Potter and the Half-Blood Prince' and 'Harry Potter and the Deathly Hallows'.",
  "og:title": "Harry Potter Adult Paperback Boxed Set: Adult Edition (Paperback)",
  "book:release_date": "2008-10-06",
  "book:author": "J. K. Rowling",
  "book:isbn": "9780747595847"
 }
]
}
},
 {
"pagemap": {
"metatags": [
 {
  "og:description": "Offers an in-depth look at the craftsmanship, artistry, technology, and more than ten-year journey that took the world's bestselling fiction from page to screen. From elaborate sets and luxurious costumes to advanced special effects and film making techniques, this title chronicles all eight films.",
  "og:title": "Harry Potter: Page to Screen (Hardback)",
  "book:release_date": "2011-10-25",
  "book:author": "Bob McCabe",
  "book:isbn": "9780857687753"
 }
 ]
}
 }
 ]
}

任何輸入將不勝感激。 請以簡單的方式告訴我,因為我是新手(剛接觸NetBeans已有2個月)。 非常感謝!!!

如果您查看json items是一個數組,則對應的java表示是一個List。 因此,您需要以List獲取items

List items = (List ) data.get("items");

同樣的邏輯也適用於metatags

List metatags = (List) pagemap.get("metatags");

通過上述更改,請嘗試以下操作

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> data = mapper.readValue(file, Map.class);
List<Map<String, Object>> items = (List) data.get("items");
for (Map<String, Object> item : items) {
    Map pagemap = (Map) item.get("pagemap");
    List<Map<String, Object>> metatags = (List) pagemap.get("metatags");
    for (Map<String, Object> tag : metatags) {
        String b_title = (String) tag.get("og:title");
        System.out.println(b_title);
    }

}

創建一個Java類,即item.java並編寫該行

Item item= mapper.readValue(new File(theJsonString.getBytes()), Item .class);

然后閱讀json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM