JSON parsing using Gson for Java

This is simple code to do it, I avoided all checks but this is the main idea. public String parse(String jsonLine) { JsonElement jelement = new JsonParser().parse(jsonLine); JsonObject jobject = jelement.getAsJsonObject(); jobject = jobject.getAsJsonObject(“data”); JsonArray jarray = jobject.getAsJsonArray(“translations”); jobject = jarray.get(0).getAsJsonObject(); String result = jobject.get(“translatedText”).getAsString(); return result; } To make the use more generic – … Read more

Unable to create converter for my class in Android Retrofit library

If anyone ever comes across this in the future because you are trying to define your own custom converter factory and are getting this error, it can also be caused by having multiple variables in a class with a misspelled or the same serialized name. IE: public class foo { @SerializedName(“name”) String firstName; @SerializedName(“name”) String … Read more

GSON – Date format

It seems that you need to define formats for both date and time part or use String-based formatting. For example: Gson gson = new GsonBuilder() .setDateFormat(“EEE, dd MMM yyyy HH:mm:ss zzz”).create(); or using java.text.DateFormat Gson gson = new GsonBuilder() .setDateFormat(DateFormat.FULL, DateFormat.FULL).create(); or do it with serializers: I believe that formatters cannot produce timestamps, but this … Read more

Gson – convert from Json to a typed ArrayList

You may use TypeToken to load the json string into a custom object. logs = gson.fromJson(br, new TypeToken<List<JsonLog>>(){}.getType()); Documentation: Represents a generic type T. Java doesn’t yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at … Read more

GSON throwing “Expected BEGIN_OBJECT but was BEGIN_ARRAY”?

The problem is you’re telling Gson you have an object of your type. You don’t. You have an array of objects of your type. You can’t just try and cast the result like that and expect it to magically work 😉 The User guide for Gson Explains how to deal with this: https://github.com/google/gson/blob/master/UserGuide.md This will … Read more

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