0

I am trying to deserialize a string which is a JSON Array in Java using fasterxml. And I want to convert this into an ArrayList of JSONObjects.

[
  {
    "test": "hello"
  },
  {
    "anotherTest": "world"
  }
]

When I try to use the object mapper as follows,

ArrayList<JSONObject> list = mapper.readValue(sourceString, ArrayList.class);

I am getting an ArrayList which contains LinkedHashMap.

I tried to change my type using the below code.

ArrayList<JSONObject> list = mapper.readValue(s, mapper.getTypeFactory().constructCollectionType(List.class, JSONObject.class));

And even this...

ArrayList<JSONObject> list = mapper.readValue(s,new TypeReference<List<JSONObject>>() {});

Both it did not help me.

Any thoughts?

Halley
  • 521
  • 10
  • 35
  • Possible duplicate of [How to use Jackson to deserialise an array of objects](https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects) – Jacob B. Dec 20 '17 at 00:25
  • btw, what this article is saying is to use a List instead of an ArrayList – Jacob B. Dec 20 '17 at 00:26
  • @JacobB. I referred the above answer. And tried the approaches in the answer.Here, my problem is, I don't have a POJO. I want the element to be a simple JSONObject. – Halley Dec 20 '17 at 00:28

0 Answers0