How to fix Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Advertisements I have encountered a problem when I want to fetch my data to recyclerview using Java, and resulting in the problem: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ My API Response : [ { "id": "1", "type": "Barang Elektronik", "payment_method": "Cash", "price": "500000", "date": "2023-07-11 00:00:00", "detail":… Read More How to fix Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

How to deserialize String to java Instant

Advertisements I have a a JSON string as : { "personId": "person1", "userId": "user1" "details": [ { "homeId": "home1", "expireAt": "2023-03-08T15:17:04.506Z" } ] } And @Data @Builder public class MyResponse { private String personId; private String userId; private List<Details> details; } @Data @Builder public class Details { private String homeId; private Instant expireAt; } I… Read More How to deserialize String to java Instant

Convert JSON to MAP using GSON library

Advertisements I want to convert JSON response to Map what is the best approach to get the desired output using GSON library. I try this and I’m getting only the ArrayList value. Map<String, Object> map = gson.fromJson(response, HashMap.class); ArrayList responseOptions = (ArrayList) map.get("data"); output: [{language=Java, value=8}, {language=Ruby, value=7}, {language=Python, value=7}] Sample JSON Response { "data":[… Read More Convert JSON to MAP using GSON library

How do I write a JSON file using GSON in the resources folder without using src/main/resources?

Advertisements I’m trying to write a JSON file with GSON in the resources folder without using src/main/resources: package repository; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import org.tinylog.Logger; import java.io.*; import java.util.List; public class GsonRepository<T> extends Repository<T> { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); public GsonRepository(Class<T> elementType) { super(elementType); } public void saveToFile(String resourceName)… Read More How do I write a JSON file using GSON in the resources folder without using src/main/resources?

Unable to convert class to JSON using GSON

Advertisements When i’m trying to convert an object in my Android App to a Json i get the following error: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations The issue is that in some devices all is working GSON version: implementation ‘com.google.code.gson:gson:2.9.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.9.0′ While the code i’m trying to convert the object is the… Read More Unable to convert class to JSON using GSON

How to structure class for deserializing this nested array?

Advertisements I’m trying to deserialize a nested array from a JSON response. It’s the first time I’ve ever gotten an array of arrays and I’m not quite sure how to structure my class to handle it. { "prices": [ [ 1641670404234, 0.01582586939240936 ], [ 1641674037525, 0.015999047707867396 ], [ 1641677655158, 0.016072905257982606 ] … ], } If… Read More How to structure class for deserializing this nested array?