简体   繁体   中英

How to parse the given string to map of string of map of string of object?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM