0

Hi my input string look like this

{
   6138249={
              value=[multi2, multi3, multi4], 
              key=TestMulticat
           }, 
   6161782={
              value=Traps (Bamboo / Box), 
              key=Observation gear
           }
}

I want to map this input string in Map<String,Map<String,Object>> in java.

As the input look more mysterious to me, i am not able to figure out the way to do the same. I tried ObjectMapper class from jackson but still not able to map. The code i write look like this

Map<String,Map<String,Object>> data=objectMapper.readValue(singledoc, Map.class);

Can somebody suggest me either approach to do this or solution, both will be equally helpful.

Techno Cracker
  • 453
  • 8
  • 26
user2903536
  • 1,716
  • 18
  • 25
  • So `value` can be either a string or an array of strings? That's impolite of the implementer. – chrylis -cautiouslyoptimistic- May 09 '18 at 05:59
  • @chrylis yes i know thats impolite. Is there a way to do this ? – user2903536 May 09 '18 at 06:15
  • Maybe [this answer](https://stackoverflow.com/a/24012023/9223839) can be of help here, it is not jackson but I still think it can be helpful. Basically you read your data as a Map and then you iterate though all values in the map and use instances to see if it is an array or a simple string. – Joakim Danielson May 09 '18 at 06:23

1 Answers1

0

Your input doesn't look like valid json, as it has unquoted string values. Json would look like this:

{
6138249:{
          value:["multi2", "multi3", "multi4"], 
          key:"TestMulticat"
       }, 
6161782:{
          value="Traps (Bamboo / Box)", 
          key="Observation gear"
       }
}

For such input you can use jackson:

objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
Map<String, Map<String, Object>> result = objectMapper
       .readValue(test, new TypeReference<Map<String, Map<String, Object>>>() {});

ALLOW_UNQUOTED_FIELD_NAMES will help you to deal with unquoted field names, however there is no such option for unqouted values.

In your case as its not a json, you can either fix your serialization to produce valid jackson or write your own Deserializer for jackson handling this, because currently its not possible to read json with unquoted string values.

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38