如何使用 jackson 将 json 数组转换为 java hashmap
How to convert json array to java hashmap using jackson
我想使用 jackson 将下面的 json 数组转换为 java hashmap 并迭代如下值:
需要这样的输出:
key Value
1 sql
2 android
3 mvc
JSON 示例:
在此处输入代码
{
"menu": [
{
"1": "sql",
"2": "android",
"3": "mvc"
}
]
}
如果有人可以分享代码来实现这一点,我们将不胜感激。
感谢您的帮助!
这是一个揭示想法的解决方案:
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);
}
}
我想使用 jackson 将下面的 json 数组转换为 java hashmap 并迭代如下值:
需要这样的输出:
key Value
1 sql
2 android
3 mvc
JSON 示例: 在此处输入代码
{
"menu": [
{
"1": "sql",
"2": "android",
"3": "mvc"
}
]
}
如果有人可以分享代码来实现这一点,我们将不胜感激。
感谢您的帮助!
这是一个揭示想法的解决方案:
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);
}
}