方式一:已数组的方式封装list集合封装json数组格式
/**
* list集合封装json数组格式
*
* 返回格式: list[{"mchBizType":"DE","mchNo":"1001772750"}]
*/
import com.fasterxml.jackson.databind.ObjectMapper;
@Test
public void test2() throws Exception {
Map<String, String> inputParams = new HashMap<String, String>();
List<Map<String, String>> list = new ArrayList<>();
inputParams.put("mchNo", "1001772750");
inputParams.put("mchBizType", "DE");
String inputParamsStr = objectMapper.writeValueAsString(inputParams);
list.add(inputParams);
String aa = objectMapper.writeValueAsString(list);
//list[{"mchBizType":"DE","mchNo":"1001772750"}]
System.out.println("list" + aa);
}
方式二:map集合封装json数组格式,大括号方式
/**
* map集合封装json数组格式
*
* 返回格式: map{"{\"mchBizType\":\"DE\",\"mchNo\":\"1001772750\"}":{"mchBizType":"DE","mchNo":"1001772750"}}
*/
import com.fasterxml.jackson.databind.ObjectMapper;
@Test
public void test3() throws Exception {
Map<String, String> inputParams = new HashMap<String, String>();
Map<String, Object> map = new HashMap<>();
inputParams.put("mchNo", "1001772750");
inputParams.put("mchBizType", "DE");
String inputParamsStr = objectMapper.writeValueAsString(inputParams);
map.put(inputParamsStr, inputParams);
String aa = objectMapper.writeValueAsString(map);
// map{"{\"mchBizType\":\"DE\",\"mchNo\":\"1001772750\"}":{"mchBizType":"DE","mchNo":"1001772750"}}
System.out.println("map" + aa);
}