0

Using this code

Map<String,Object> payloadMap = new HashMap<String,Object>();
payloadMap = (Map<String,Object>) new Gson().fromJson(result, payloadMap.getClass());

, I convert this json:

{ "name":"name1", "job":"prosecutor", "department": { "department_name":"prosecutor's office" } }

to the map (map with unlimited number of child maps): enter image description here

This done well, but now I want to get an access to values of child (nested) maps.

In parent map child maps "wrapped" to Object. So, I tried to get "wrapped" child maps from the Object-values of parent map.

  public void mapRequestNode (Map<String,Object> payloadMap) {
    payloadMap.entrySet().forEach(node->this.getDataFromNode(node));
  }

enter image description here

As you can see from the above picture, there are no way to use child map "department", which had been "wrapped" to Object. I have an access to Object-methods, but not to the Map-methods (for example, I cant use "value.get("department_name")". I tried cast "(Map<String, Object>)value", but without success...

The "department" name in case above is only for example! I dont know concrete name of json child-objects. There may be unlimited number of names! So I cant use something like this "payloadMap.get("department")"

3
  • 3
    X instanceof Object is a redundant check. Everything is Commented Sep 27, 2020 at 16:40
  • Sorry, but what do you mean by "Everything is"? Commented Sep 27, 2020 at 17:06
  • 1
    Everything is an object. So it cannot be false Commented Sep 27, 2020 at 18:47

3 Answers 3

1

Following

((Map<String, Object>)payloadMap.get("department")).get("department_name")

should work, dont?

Sign up to request clarification or add additional context in comments.

6 Comments

Probably, this way may be applied in case, when have only one child map with well-known name. But my task is to iterate over the unlimited (custom) number of child of maps.
maybe I dont understand you, but why dont use something like if (value.containsKey("department_name")) However, of course, better practice would be using GSON in better way to parse it not to maps but objects, and then check for base classes/interfaces etc...
Michal Drozd, In my case I cant know concrete name of json-object. It may be any, absolutely any name
but it doesn't make any sense for me what you are trying to do. What do you want get from these unknown objects?
I want to get the value from these any objects. For example, for ""department_name":"prosecutor's office"". I want know "prosecutor's office"
|
1

Your variable value is of type Object, which means that the compiler will not know anything else about the variable. Even if the object you retrieve from your json file is a map, as you store it in a Object variable, the compiler will handle it as an Object and not as a Map. That is why you cannot do value.get("department"); : the method get does not exist for the type Object.

You need to cast whatever is stored in value.get("department") as a Map<String, Object> to be able to handle it as a Map.

4 Comments

In my case I cant know concrete name of json-object. It may be any, absolutely any name. So, I dont (and must not) know, which it be "department" or any other key!
Then perhaps a solution would be to try to read it as a Map and catch the exception if it cannot be read as a Map, for example : try { new ObjectMapper().readValue(value.get("department"), Map.class); } catch (Exception e) {}, it is far from ideal but it should work.
Thank you. Look, please, how I answered my question.
From what I understand of the answer, you're just handling it with another approach but the problem is still here. Have you tried using JSONObject instead of Map and Gson ? This way, you don't have to cast it to Map and can directly access the data in the format that you want, and each subobject can be access the same way recursively. That is actually the only solution coming to my mind if you don't know the format of the Json file.
0

I have found a special solution.

I convert json to Map<String,Map<String,Object>>. Not in Map<String,Object>. In this case I can successfully use child-maps of parent dto-map.

But this solution is special (not general) in the meaning that, I can handle in this way json, which consist only of objects.

For example, if I try to get the value of "job" in following example:

{
   "job": "prosecutor",
  "department": {
                 "department_name":"prosecutor's office"
                 }
}  

by using Map.Entry<String, Map<String, Object>> payloadNodeEntry.getValue, I will receive ClassCastException (cant cast String to Map<String, Object> ).

Comments

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.