Il mio esempio di esempio: Abbiamo un tipo di object di Apple. Apple ha alcune variabili membro: String appleName; // The apples name String appleBrand; // The apples brand List seeds; // A list of seeds the apple has E l’object seme appare come segue. String seedName; // The seeds name long seedSize; // The […]
Ho interi nel mio json, e non voglio che Gson li converta in doppio. Quanto segue non funziona: @Test public void keepsIntsAsIs(){ String json=”[{\”id\”:1,\”quantity\”:2,\”name\”:\”apple\”},{\”id\”:3,\”quantity\”:4,\”name\”:\”orange\”}]”; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Double.class, new DoubleSerializerAsInt()); Gson gson = gsonBuilder.create(); List<Map> l = gson.fromJson(json, List.class); for(Map item : l){ System.out.println(item); } } private static class DoubleSerializerAsInt implements JsonSerializer{ @Override […]
Sto arrivando sotto l’errore. Imansible analizzare JSON a causa di: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_ARRAY previsto, ma era BEGIN_OBJECT alla riga 1 colonna 2 URL server public static final String SERVER_URL = “https://maps.googleapis.com/maps/api/timezone/json?location=-37.8136,144.9631×tamp=1389162695&sensor=false”; Esegui la richiesta try { // Create an HTTP client HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(SERVER_URL); // Perform the request and […]
Mi chiedevo se da qualche parte là fuori esistesse una libreria java in grado di interrogare un object JSONObject. Più in profondità sto cercando qualcosa come: String json = “{ data: { data2 : { value : ‘hello’}}}”; … // Somehow we managed to convert json to jsonObject … String result = jsonObject.getAsString(“data.data2.value”); System.out.println(result); Mi […]
Io uso la libreria Gson per convertire oggetti Java in una risposta Json … il problema è che dopo una richiesta JPA l’object recuperato da DB non può essere convertito a causa di una relazione ricorsiva con altre quadro ( vedi la mia domanda precedente ) per esempio: public class Gps implements Serializable { private […]
Ho un JSON molto semplice con recensioni per prodotti come: { “reviewerID”: “A2XVJBSRI3SWDI”, “asin”: “0000031887”, “reviewerName”: “abigail”, “helpful”: [0, 0], “unixReviewTime”: 1383523200, “reviewText”: “Perfect red tutu for the price. “, “overall”: 5.0, “reviewTime”: “11 4, 2013”, “summary”: “Nice tutu” } { “reviewerID”: “A2G0LNLN79Q6HR”, “asin”: “0000031887”, “reviewerName”: “aj_18 \”Aj_18\””, “helpful”: [1, 1], “unixReviewTime”: 1337990400, “reviewText”: “This […]
Sto leggendo una risposta JSON con Gson , che restituisce somtimes a NumberFormatException perché un valore int previsto è impostato su una stringa vuota. Ora mi chiedo quale sia il modo migliore per gestire questo tipo di eccezione. Se il valore è una stringa vuota, la deserializzazione dovrebbe essere 0. Risposta JSON prevista: { “name” […]
Devo interagire con un’API e il formato della risposta (da quello che ho letto) sembra essere scarsamente strutturato. Ho trovato che i gruppi di google rispondono a un problema un po ‘simile qui , ma sto riscontrando problemi nell’implementazione di una class Response per gestire Gson.fromJson. C’è un esempio che mi manca è là fuori? […]
Si prega di consigli su come convertire una String in JsonObject usando la libreria gson . Cosa faccio senza successo: String string = “abcde”; Gson gson = new Gson(); JsonObject json = new JsonObject(); json = gson.toJson(string); // Can’t convert String to JsonObject
Non ho mai fatto molto con la serializzazione, ma sto cercando di usare Google Gson per serializzare un object Java su un file. Ecco un esempio del mio problema: public interface Animal { public String getName(); } public class Cat implements Animal { private String mName = “Cat”; private String mHabbit = “Playing with yarn”; […]