使用Gson将问题从JSON转换为POJO

时间:2017-04-13 06:24:45

标签: java json gson

我有一个JSON REST端点响应,我希望从中获取hotelNbr的值。我该怎么做?

{
  "found": [
    {
      "hotelNbr": 554050442,
      "hotelAddress": "119 Maven Lane, New Jersey",
    }
  ],
  "errors": []
}

我使用下面的代码来获取它,但它在下面提到的行中失败了:

public List<Hotel> RDS_POST_HotelDetails(String HotelName, String sUrl) throws Exception, IOException {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();

            // Create your http client
            CloseableHttpClient httpclient = HttpClientBuilder.create().build();

            // Create http Put object
            HttpPost ohttppost = new HttpPost(sUrl);

            // Message Body

            StringEntity input = new StringEntity(

                    "{\"hotelNbr\":[\""+HotelName+"\" ]}"
                    );

            // Set content type for post
            input.setContentType("application/json");

            // attach message body to request
            ohttppost.setEntity(input);

            // submit request and save response
            HttpResponse response = httpclient.execute(ohttppost);

            // Get response body (entity and parse to string
            String sEntity = EntityUtils.toString(response.getEntity());

                List<Hotel> hotelobject = new ArrayList<Hotel>();

                // Create a type token representing the type of objects in your json response
                // I had to use the full class path of TYPE because we also have a  Type class in our project
                java.lang.reflect.Type cType = new TypeToken<List<Hotel>>() {
                }.getType();

                // Convert to Array object using gson.fromJson(<json string>,
                // <type>)
                hotelObject = gson.fromJson(sEntity, cType); // I am getting error here 

                String hotelNumber = hotelObject.get(0).getFound().get(0).getItemNbr().toString();

}

请找到下面的Hotel.java课程

package com.hotels.company;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Hotel {

    @SerializedName("found")
    @Expose
    private List<Found> found = null;
    @SerializedName("errors")
    @Expose
    private List<Object> errors = null;

    public List<Found> getFound() {
        return found;
    }

    public void setFound(List<Found> found) {
        this.found = found;
    }

    public List<Object> getErrors() {
        return errors;
    }

    public void setErrors(List<Object> errors) {
        this.errors = errors;
    }

}

请在下面找到Found.java课程:

package com.hotels.company;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Found {

    @SerializedName("hotelNbr")
    @Expose
    private Integer hotelNbr;
    @SerializedName("hotelAddress")
    @Expose
    private String hotelAddress;

    public Integer getHotelNbr() {
        return hotelNbr;
    }

    public void setHotelNbr(Integer hotelNbr) {
        this.hotelNbr = hotelNbr;
    }

    public String getHotelAddress() {
        return hotelAddress;
    }

    public void setHotelAddress(String hotelAddress) {
        this.hotelAddress = hotelAddress;
    }

}

我尝试在StackOverflow问题中找到一些例子但是没有得到我的解决方案。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您正在解析的JSON格式不正确..

“hotelAddress”删除

后有一个逗号

正确的JSON将是:

 {  
   "found":[  
      {  
         "hotelNbr":554050442,
         "hotelAddress":"119 Maven Lane, New Jersey"
      }
   ],
   "errors":[ ]
}

答案 1 :(得分:0)

我发现了几个问题:

  1. Json无效。观察"hotelAddress": "119 Maven Lane, New Jersey",末尾有一个逗号。删除它。

  2. 您正在尝试将json反序列化为List<Hotel>,但提到的json不是列表。更新json或将其反序列化为Hotel对象而不是List

相关问题