简体   繁体   中英

How to Parse Json response from REST webserivce using java

I have REST webservice which gives response in Json format, I have locally assigned the Json response in a variable. But now I want to know if we can parse and how. Below is a response from webservice.

{
  "actionresult": "successful",
  "licenceDetail": [
    {
      "licence": "SA123",
      "type": "SZ Abalone",
      "pendingtrip": [

      ],
      "Vessel": [
        {
          "name": "Red Fire",
          "number": "SA123"
        }
      ],
      "defaultvalue": {
        "LandingPort": "Anxious Bay",
        "DepartPort": "Acramans Creek",
        "Vessel": "SA123",
        "AreaFishing": "SA"
      }
    },
    {
      "licence": "K01",
      "type": "Blue Crab",
      "pendingtrip": [

      ],
      "Vessel": [
        {
          "name": "Abrolhos Spirit",
          "number": "K01"
        }
      ],
      "defaultvalue": null
    }
  ]
}

Any help will be appreciated. Regards, Rohit

使用Jackson2库将json字符串转换为Object类。

I always use Gson. This is very easy to use.

Gson gson = new GsonBuilder().create();
MyClass myClass = gson.fromJson(jsonString, MyClass.class);

Maven dependency

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.4</version>
    </dependency>

here you can read it in detail ObjectJson

   JsonReader rdr = Json.createReader(is)) {

 JsonObject obj = rdr.readObject();
  JsonArray results = obj.getJsonArray("licenceDetail");
 for (JsonObject result : results.getValuesAs(JsonObject.class)) {
 String Landing   result.getJsonObject("Default Value").getString("Landing port");

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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