2017-04-13 41 views
-1

我有一個JSON REST端點響應,我想從中獲取hotelNbr的值。我該怎麼做 ?使用Gson轉換從JSON到POJO的問題

{ 
    "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(); 

} 

請在下面找到

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; 
    } 

} 

的Hotel.java類請找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的問題中找到一些例子,但沒有得到我的解決方案。任何幫助將不勝感激。

+1

什麼是錯誤堆棧? –

回答

0

你正在分析的格式不正確的JSON ..

有 「hotelAddress」 之後的逗號刪除

正確的JSON是:

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

我發現一對夫婦的問題:

  1. Json無效。觀察"hotelAddress": "119 Maven Lane, New Jersey",末尾有一個逗號。去掉它。

  2. 您試圖將json反序列化爲List<Hotel>,但提到的json不是列表。請更新json或將其反序列化爲Hotel對象,而不是List

+0

我從JSON中刪除了,但仍然出現同樣的問題 – puchu