Convert JSONArray to List<HashMap<String,Object>> in java without GSON

Mohammed Idris :

I have a JSONArray from net.minidev.json which I want to convert to List<HashMap<String,Object>>.

There are many answers for converting the JSONArray using Gson.

However, I cannot add Gson as a dependency to my pom.xml, so I am looking for a way to achieve it with Java-8 features.

My JSONArray is something like this: It comprises multiple hierarchies.

[
  {
    "data": {
      "name": "idris"
    },
    "children": [
      {
        "processStandardDeduction": 69394,
        "cropId": 1,
        "data": null,
        "expectedQuantityPerAcre": 1220,
        "name": "Red Tomato 1 quality",
        "id": 1003,
        "autoArchivePlotDays": 59902
      },
      {
        "processStandardDeduction": 69394,
        "cropId": 1,
        "autoArchivePlotDays": 59902
      },
      {
        "processStandardDeduction": 69394,
        "cropId": 1,
        "autoArchivePlotDays": 59902
      }
    ],
    "name": "Red Tomato",
    "id": 1002
  },
  {
    "data": null,
    "name": "Red Tomato 1 quality",
    "id": 1003,
    "processStandardDeduction": 69394,
    "cropId": 1,
    "expectedQuantityPerAcre": 1220,
    "cropName": "Tomato",
    "autoArchivePlotDays": 59902
  },
  {
    "data": null,
    "name": "Red Tomato 3 quality",
    "id": 1001,
    "processStandardDeduction": 69394,
    "autoArchivePlotDays": 59902
  },
  {
    "processStandardDeduction": 69394,
    "cropId": 1,
    "data": null,
    "id": 1004,
    "autoArchivePlotDays": 59902
  }
]

I would like to achieve same structure in List>

I tried looping each element of the JSONArray by converting it to each HashMap<String,Object> and then adding it to the List<HashMap<String,Object>>.

ObjectMapper mapper = new ObjectMapper();
List<HashMap<String, Object>> cropDetailsList = new ArrayList<>();
        for (Object eachCropJson : cropDetails) { //cropDetails is the JSONArray

            HashMap<String, Object> eachCropMap = (HashMap<String, Object>) mapper.convertValue(eachCropJson,
                    HashMap.class);
            cropDetailsList.add(eachCropMap);
        }
return cropDetailsList;

I would like to try a better approach using Java-8 features without using a forEach. Thanks in advance.

Deadpool :

If you get this JSON as String then you can use ObjectMapper.readValue method

readValue(String content, TypeReference valueTypeRef)

Code

List<HashMap<String, Object>> cropDetailsList = mapper.readValue(jsonString,
                                   new TypeReference<List<HashMap<String, Object>>>(){});

In the same way if you want to iterate JSONArray

List<HashMap<String, Object>> cropDetailsList = cropDetails.stream()
            .map(eachCropJson->(HashMap<String, Object>) mapper.convertValue(eachCropJson, HashMap.class))
            .collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

GSON - convert a string into a JsonArray

How to convert JSONArray to List with Gson?

How to convert automatically list of objects to JSONArray with Gson library in java?

Java: Loop through JSON string (cant convert to JsonArray (gson)

How to convert a jsonArray into List<String> Java?

Java: Convert an Object List into a hashmap inside of another hashmap

Convert List<HashMap<String, Object>> to stream

convert String to JSONArray using java

Java: how to convert HashMap<String, Object> to array

Convert String to Hashmap in Java

convert TreeMap<String,Object> to List<HashMap<String,Object>>

convert hashmap items into another object and return as a list in java

Convert List<Object> to String[] in Java

Kotlin convert List with nullables to HashMap without nullables

Kotlin convert json string to list of object using Gson

Convert to list of data class object from JSON string using Gson?

How to convert specific JSONArray to JAVA object?

convert scala hashmap with List to java hashmap with java list

How to convert JSONArray to java string array

Error when trying to convert Java object to JSON string using GSON

How to convert List to a JSON Object using GSON?

How to convert String into Hashmap in java

Gson - Convert from json string to list of objects

How to convert JsonArray from Gson to stream

Java-8 JSONArray to HashMap

Get List<String> and HashMap<String, Object> using Java 8 streams

How does one convert a HashMap to a List in Java?

How to convert List<NameValuePair> into a hashMap<String, String>?

How to Convert String to List<String> in Java Without using Jackson Api