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

{  

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


慕工程0101907
浏览 129回答 2
2回答

噜噜哒

使用 Gson API。JsonParser parser=new JsonParser();JsonObject jsonObj=(JsonObject) parser.parse("Your Json String");String first=jsonObj.get("first");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// first and second are String in jsonString second=jsonObj.get("second");JsonObject fourth=jsonObj.getAsJsonObject("fourth");&nbsp; // because fourth is Object in json// And so on......

九州编程

首先,您必须为您的 JSON 准备一个匹配的类结构。如果完成,在您的代码中实例化一个 ObjectMapper 并从中获取对象。ObjectMapper mapper = new ObjectMapper();YourType unmarshalledJson = mapper.readValue(jsonString,YourType.class);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java