2017-04-13 1 views
-1

J'ai une réponse de point de terminaison JSON REST et je voulais obtenir la valeur de hotelNbr. Comment fait-on ça ?Problème de conversion de JSON en POJO en utilisant Gson

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

J'utilise le code ci-dessous pour l'obtenir, mais il échoue en dessous de la ligne mentionnée:

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

} 

S'il vous plaît trouver la classe Hotel.java ci-dessous

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

} 

S'il vous plaît trouver Found.java classe ci-dessous:

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

} 

J'ai essayé de trouver des exemples dans les questions de StackOverflow mais je n'ai pas trouvé de solution pour le mien. Toute aide serait appréciée.

+1

Quelle est la pile d'erreurs? –

Répondre

0

Le JSON vous analyse syntaxique est pas bien formaté ..

Il y a une virgule après "hotelAddress" supprimer cette

JSON correcte serait:

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

J'ai trouvé quelques problèmes:

  1. Json n'est pas valide. Observez qu'il y a une virgule à la fin de "hotelAddress": "119 Maven Lane, New Jersey",. Retirez-le.

  2. Vous essayez de désérialiser le json en List<Hotel>, mais le json mentionné n'est pas une liste. Mettez à jour le fichier json ou désérialisez-le en objet Hotel au lieu de List.

+0

J'ai retiré le, du JSON, mais toujours le même problème – puchu