skip to Main Content

I have the following String.

respData = "[{"pmtInd":"C"},{"pmtInd":"D"},{"pmtInd":"G"},{"pmtInd":"C"},{"pmtInd":"B"}]"

I am trying to convert this into a List<Map<String, Object>>. However, I get some failures.

Here are some things I have tried.

List<Map<String, Object>> walletDetailsList = new ArrayList<>();
try {
    ObjectMapper objectMapper = new ObjectMapper();

    // objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    String respData = objectMapper.writeValueAsString(globalSession.getValue(id + "walletAccounts", String.class));

    // JSONArray arr = new JSONArray(respData);
    //walletDetailsList = objectMapper.readValue(arr, String.class);
    walletDetailsList = new Gson().fromJson(respData, new ArrayList<Map<String, String>>().getClass());

} catch (Exception e) {
    LOGGER.info("Exception getting global session for Wallet: {}", e.getMessage());
}

The above code gives me this error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $

List<Map<String, Object>> walletDetailsList = new ArrayList<>();
try {
    ObjectMapper objectMapper = new ObjectMapper();

    objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    String respData = objectMapper.writeValueAsString(globalSession.getValue(id + "walletAccounts", String.class));

    // JSONArray arr = new JSONArray(respData);
    walletDetailsList = objectMapper.readValue(arr, String.class);
    //walletDetailsList = new Gson().fromJson(respData, new ArrayList<Map<String, String>>().getClass());

} catch (Exception e) {
    LOGGER.info("Exception getting global session for Wallet: {}", e.getMessage());
}

The above code gives me this…

"walletDetailsList": [{"pmtInd":"C"},{"pmtInd":"D"},{"pmtInd":"G"},{"pmtInd":"C"},{"pmtInd":"B"}]

It’s 1 size instead of 5.

enter image description here

What is the best way to tackle this?

2

Answers


  1. Modify to this:

    ObjectMapper mapper = new ObjectMapper();
    List<Map<String, Object>> walletDetailsList = mapper.readValue("[{"pmtInd":"C"},{"pmtInd":"D"},{"pmtInd":"G"},{"pmtInd":"C"},{"pmtInd":"B"}]", List.class);
    System.out.println(walletDetailsList);
    

    It will work.

    Output: [{pmtInd=C}, {pmtInd=D}, {pmtInd=G}, {pmtInd=C}, {pmtInd=B}]

    Login or Signup to reply.
  2. Prefer using fromJson(String, TypeToken) since its return type is
    based on the TypeToken and is therefore more type-safe. This method is
    useful if the specified object is a generic type.

    So I converted Json String to List<Map<Stirng, Object>> using TypeToken. My code is:

    String respData = "[{"pmtInd":"C"},{"pmtInd":"D"},{"pmtInd":"G"},{"pmtInd":"C"},{"pmtInd":"B"}]";
    Type type = new TypeToken<List<Map<String, Object>>>() {
    }.getType();
    List<Map<String, Object>> walletDetailsList = gson.fromJson(respData, type);
    

    The output of my code is: [{pmtInd=C}, {pmtInd=D}, {pmtInd=G}, {pmtInd=C}, {pmtInd=B}]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search