且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jackson将json数组转换为java hashmap

更新时间:2022-10-17 21:09:45

这是一个解决方案,它揭示了这个想法:

  public class JacksonSerializer {

public static final String INPUT ={\ n+
\menu\:[\\\
+
{\ n+
\1 \:\sql \,\\\
+
\2 \ :\android \,\\\
+
\3 \:\mvc\\\\
+
} \ n +
] \\\
+
};

public static class MenuItems {

Map< String,String> menu = Maps.newHashMap();
}


public static class MenuItemsDeserializer extends JsonDeserializer< MenuItems> {


@Override
public MenuItems反序列化(org.codehaus.jackson.JsonParser jsonParser,
DeserializationContext deserializationContext)
抛出IOException,JsonProcessingException {

JsonNode node = jsonParser.getCodec()。readTree(jsonParser);

final JsonNode elems = node.getElements()。next()。getElements()。next();
最终地图< String,String> map = Maps.newHashMap();
final Iterator< Map.Entry< String,JsonNode>> it = elems.getFields();
while(it.hasNext()){
final Map.Entry< String,JsonNode> entry = it.next();
map.put(entry.getKey(),entry.getValue()。asText());
}

MenuItems menuItems = new MenuItems();
menuItems.menu = map;
return menuItems;



public static void main(final String [] args)throws IOException {

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule(SimpleModule,
new Version(1,0,0,null));
module.addDeserializer(MenuItems.class,new MenuItemsDeserializer());
mapper.registerModule(module);

MenuItems menuItems = mapper.readValue(INPUT,MenuItems.class);

}
}


I want to convert below json array to java hashmap using jackson and iterate the values like below:

Need output like this:

key  Value 
1    sql
2    android
3    mvc

JSON Sample: enter code here

{
    "menu": [
        {
            "1": "sql",
            "2": "android",
            "3": "mvc"
        }
    ]
}

It would be really appreciated if someone can share the code to achieve this.

Thanks for your help!

Here is a solution that reveals the idea:

public class JacksonSerializer {

    public static final String INPUT = "{\n" +
            "    \"menu\": [\n" +
            "        {\n" +
            "            \"1\": \"sql\",\n" +
            "            \"2\": \"android\",\n" +
            "            \"3\": \"mvc\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

    public static class MenuItems {

        Map<String, String> menu = Maps.newHashMap();
    }


    public static class MenuItemsDeserializer extends JsonDeserializer<MenuItems> {


        @Override
        public MenuItems deserialize(org.codehaus.jackson.JsonParser jsonParser,
                                               DeserializationContext deserializationContext)
                throws IOException, JsonProcessingException {

            JsonNode node = jsonParser.getCodec().readTree(jsonParser);

            final JsonNode elems = node.getElements().next().getElements().next();
            final Map<String, String> map = Maps.newHashMap();
            final Iterator<Map.Entry<String, JsonNode>> it = elems.getFields();
            while (it.hasNext()) {
                final Map.Entry<String, JsonNode> entry = it.next();
                map.put(entry.getKey(), entry.getValue().asText());
            }                

            final MenuItems menuItems = new MenuItems();
            menuItems.menu = map;
            return menuItems;
        }
    }

    public static void main(final String[] args) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule("SimpleModule",
                new Version(1,0,0,null));
        module.addDeserializer(MenuItems.class, new MenuItemsDeserializer());
        mapper.registerModule(module);

        MenuItems menuItems = mapper.readValue(INPUT, MenuItems.class);

    }
}