我想构建一个像这样的JSON:
{
"Id": "33396",
"Actions": [
{
"Key": "5",
"Value": "Test"
},
{
"Key": "6",
"Value": "Test 2"
}
]
}
我正在使用MultiValueMap,这是我的代码:
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("Id","33396");
我不知道如何在我的代码上实现动作列表
我这样做并且有效,但我认为这是重复的
MultiValueMap map = new LinkedMultiValueMap(); MultiValueMap mapActions0 = new LinkedMultiValueMap(); MultiValueMap mapActions1 = new LinkedMultiValueMap();
//Id
map.add("Id","33396");
//Actions
mapActions0.add("Key","5");
mapActions0.add("Value","Test");
mapActions1.add("Key","6");
mapActions1.add("Value","Test 2");
map.put("AcoesVideo",Arrays.asList(mapActions0,mapActions));
答案 0 :(得分:0)
使用HashMap
并使用Map<String, Map<String, String>>
执行此操作
Map<String, Map<String, String>> map = new HashMap<>();
Map<String, String> actions = new HashMap<>();
actions.put("5", "test");
actions.put("6", "test2");
map.put("33396", actions);
答案 1 :(得分:0)
HashMap<String, Object> innerMap1 = Maps.newHashMap();
innerMap1.put("Key", "5");
innerMap1.put("Value", "Test");
HashMap<String, Object> innerMap2 = Maps.newHashMap();
innerMap2.put("Key", "6");
innerMap2.put("Value", "Test 2");
ArrayList<Object> list = Lists.newArrayList();
list.add(innerMap1);
list.add(innerMap2);
HashMap<String, Object> map = Maps.newHashMap();
map.put("ID", "33396");
map.put("Actions", list);
System.out.println(JSON.toJSONString(map));