简体   繁体   English

在Java NetBeans中解析JSON字符串

[英]Parsing JSON string in Java NetBeans

Can anyone please tell me how to get the values of og:description , og:title , book:release_date , book:author , and book:isbn from every metatags? 谁能告诉我如何从每个元标记中获取og:descriptionog:titlebook:release_datebook:authorbook:isbn (please see the JSON content below) Eventually these values will be displayed in a table so I need them as Java objects (Strings).. (请参阅下面的JSON内容)最终,这些值将显示在表中,因此我需要将它们作为Java对象(字符串)。

So far, I have tried using many external libs but this is my first project using JSON, so I didn't understand how they worked.. 到目前为止,我已经尝试使用许多外部库,但这是我使用JSON的第一个项目,因此我不了解它们的工作原理。

For your information, I am using NetBeans and am making a restful web service. 供您参考,我正在使用NetBeans,并正在提供一个宁静的Web服务。

I have managed to convert these into a JSON string using this code: 我设法使用以下代码将它们转换为JSON字符串:

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

but when I tried to do this: 但是当我尝试这样做时:

    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);

I get this error: 我收到此错误:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map at API.GCustSearchAPI.main(GCustSearchAPI.java:77) java.lang.ClassCastException:无法将API.GCustSearchAPI.main(GCustSearchAPI.java:77)上的java.util.ArrayList强制转换为java.util.Map

line 77 is this: 第77行是这样的:

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

here is the json content: 这是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"
 }
 ]
}
 }
 ]
}

Any input will be appreciated. 任何输入将不胜感激。 Please tell me in an easy way because I am a beginner (just touched NetBeans for 2 months). 请以简单的方式告诉我,因为我是新手(刚接触NetBeans已有2个月)。 Many thanks!!! 非常感谢!!!

If you look at the json items is an array, the corresponding java representaion is a List. 如果您查看json items是一个数组,则对应的java表示是一个List。 So you need to fetch items as a List 因此,您需要以List获取items

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

The same logic is applied to metatags also 同样的逻辑也适用于metatags

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

With the above mentioned changes, try this 通过上述更改,请尝试以下操作

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);
    }

}

create a java class ie item.java and write the line 创建一个Java类,即item.java并编写该行

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

then read the json 然后阅读json

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

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