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.
What is the best way to tackle this?
2
Answers
Modify to this:
It will work.
Output:
[{pmtInd=C}, {pmtInd=D}, {pmtInd=G}, {pmtInd=C}, {pmtInd=B}]
So I converted Json String to
List<Map<Stirng, Object>>
using TypeToken. My code is:The output of my code is:
[{pmtInd=C}, {pmtInd=D}, {pmtInd=G}, {pmtInd=C}, {pmtInd=B}]