3

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.

1
  • Just using map and collecting should work cropDetails.stream().map(eachCropMap -> (Map<String, Object>) mapper.convertValue(eachCropJson, HashMap.class)).collect(Collectors.toList()) Commented Jul 23, 2019 at 9:11

2 Answers 2

4

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());
Sign up to request clarification or add additional context in comments.

8 Comments

Wouldn't conversion of a JSONArray to a String become a costly operation? If it is not, then I shall use this method. Since I am already using a mapper to convert each Object of JSONArray to a HashMap, I am not sure which approach is better in terms of performance.
I don't think it is a costly operation, toString() should work for that @MohammedIdris, but i gave you the second approach also
still you can avoid this type casting (HashMap<String, Object>) do some google search @MohammedIdris
The first approach seems to work as expected. However, the StreamSupport.stream says the method stream(Spliterator<T>, boolean) is not applicable for the arguments (JSONArray, boolean). Do I use cast (Spliterator<JSONObject>) or (Spliterator<HashMap<String,Object>>)? Also, Is Type Casting a bad idea? @Deadpool
It works as expected with the updated answer. Thanks @Deadpool
|
2

Using stream this can be done

List<HashMap<String, Object>> output = cropDetails.toList().stream().map(m -> ((HashMap<String, Object>) m)).collect(Collectors.toList());

2 Comments

cropDetails is a JSONArray. toList() function is undefined for type JSONArray. @SUMIT NAVIN
Using stream on cropDetails JSONArray gave me the expected result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.