Gson to json conversion with two DateFormat

I was facing the same issue. Here is my solution via custom deserialization: new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()); private static final String[] DATE_FORMATS = new String[] { “MMM dd, yyyy HH:mm:ss”, “MMM dd, yyyy” }; private class DateDeserializer implements JsonDeserializer<Date> { @Override public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context) throws JsonParseException { for (String format … Read more

How to compare JSON documents and return the differences with Jackson or Gson?

Reading the JSON documents as Maps and comparing them You could read both JSON documents as Map<K, V>. See the below examples for Jackson and Gson: ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> type = new TypeReference<HashMap<String, Object>>() {}; Map<String, Object> leftMap = mapper.readValue(leftJson, type); Map<String, Object> rightMap = mapper.readValue(rightJson, type); Gson gson = new … Read more

How to find specified name and its value in JSON-string from Java?

Use a JSON library to parse the string and retrieve the value. The following very basic example uses the built-in JSON parser from Android. String jsonString = “{ \”name\” : \”John\”, \”age\” : \”20\”, \”address\” : \”some address\” }”; JSONObject jsonObject = new JSONObject(jsonString); int age = jsonObject.getInt(“age”); More advanced JSON libraries, such as jackson, … Read more

Retrofit GSON serialize Date from json string into java.util.date

Gson gson = new GsonBuilder() .setDateFormat(“yyyy-MM-dd’T’HH:mm:ss”) .create(); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(API_BASE_URL) .setConverter(new GsonConverter.create(gson)) .build(); Or the Kotlin equivalent: val gson = GsonBuilder().setDateFormat(“yyyy-MM-dd’T’HH:mm:ss”).create() RestAdapter restAdapter = Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build() .create(T::class.java) You can set your customized Gson parser to retrofit. More here: Retrofit Website Look at Ondreju’s response to see how to implement this … Read more

Trying to fix NetworkOnMainThreadException but gives Toast error

You can’t do UI stuff in doInBackground(). Hence, you can’t display a Toast there. You need to move this to onPostExecute() or somewhere else. Possibly onProgressUpdate() You could call publishProgress(results) and show the Toast in onProgressUpdate() or return results to onPostExecute() and display it there. You also have the option of sending the data back … Read more

Remove empty collections from a JSON with Gson

Steps to follow: Convert the JSON String into Map<String,Object> using Gson#fromJson() Iterate the map and remove the entry from the map which are null or empty ArrayList or Map. Form the JSON String back from the final map using Gson#toJson(). Note : Use GsonBuilder#setPrettyPrinting() that configures Gson to output Json that fits in a page … Read more

The easiest way to remove the bidirectional recursive relationships?

There’s a Gson extension called GraphAdapterBuilder that can serialize objects that contain circular references. Here’s a very simplified example from the corresponding test case: Roshambo rock = new Roshambo(“ROCK”); Roshambo scissors = new Roshambo(“SCISSORS”); Roshambo paper = new Roshambo(“PAPER”); rock.beats = scissors; scissors.beats = paper; paper.beats = rock; GsonBuilder gsonBuilder = new GsonBuilder(); new GraphAdapterBuilder() … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)