为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Jackson 以最简单的形式破坏 JSON

如何使用 Jackson 以最简单的形式破坏 JSON

慕工程0101907 2021-10-13 10:15:17
{     "first":"element",   "second":"Integral",   "isThird":false,   "fourth":{        "ONE":[           {              "100":"Cars"         },         {              "200":"Truck"         }      ],      "TWO":[           {              "6":"Vintage"         },         {              "4":"Sports"         }      ]   }}我有一个 json,我在其中使用 Jackson 以 Java 对象形式中断。我想知道如何使用 Jackson 将这个 json 分解成最简单的形式。这是我使用的杰克逊依赖<dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.6.0</version></dependency>
查看完整描述

2 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

使用 Gson API。


JsonParser parser=new JsonParser();

JsonObject jsonObj=(JsonObject) parser.parse("Your Json String");


String first=jsonObj.get("first");                     // first and second are String in json

String second=jsonObj.get("second");


JsonObject fourth=jsonObj.getAsJsonObject("fourth");  // because fourth is Object in json


// And so on......


查看完整回答
反对 回复 2021-10-13
?
九州编程

TA贡献1785条经验 获得超4个赞

首先,您必须为您的 JSON 准备一个匹配的类结构。如果完成,在您的代码中实例化一个 ObjectMapper 并从中获取对象。


ObjectMapper mapper = new ObjectMapper();

YourType unmarshalledJson = mapper.readValue(jsonString,YourType.class);


查看完整回答
反对 回复 2021-10-13
  • 2 回答
  • 0 关注
  • 128 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信