java - 在 Java NetBeans 中解析 JSON 字符串

标签 java json string parsing netbeans

谁能告诉我如何获取og:descriptionog:titlebook:release_date的值每个元标记中的 book:authorbook:isbn ? (请参阅下面的 JSON 内容)最终这些值将显示在表格中,因此我需要它们作为 Java 对象(字符串)..

到目前为止,我已经尝试使用许多外部库,但这是我使用 JSON 的第一个项目,所以我不明白它们是如何工作的..

供您引用,我正在使用 NetBeans 并正在制作一个 Restful 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: java.util.ArrayList 无法转换为 java.util.Map 在 API.GCustSearchAPI.main(GCustSearchAPI.java:77)

第 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 个月)。非常感谢!!!

最佳答案

如果看jsonitems是一个数组,那么对应的java表示就是一个List。因此,您需要将 items 作为 List

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

相同的逻辑也适用于元标记

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 - 在 Java NetBeans 中解析 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15782666/

相关文章:

javascript - 如何检测和获取字符串javascript的url

java - 如何使用BufferedImage在JPanel上绘制多条曲线?

java - 使用 @ManyToMany hibernate 并更新两种方式

java - libgdx 如何检测冲突?

php - SQL 表中的 JSON 编码 -> JSON 问题

javascript - JSON 按时间过滤

java - 有没有办法控制位深度=1(黑白)的 TIFF-PNG 转换中哪些像素变黑(阈值)

java - 如何通过 JsonSystem(Itemscript JSON 库)使用内存数据存储?

java - 字符串文字和对象文字比较

python - 存储长字符串资源的最 Pythonic/Django 风格的方法是什么?