java中 使用json封装对象集合||数组方式

方式一:已数组的方式封装list集合封装json数组格式

/**
	 * list集合封装json数组格式
	 * 
	 * 返回格式: list[{"mchBizType":"DE","mchNo":"1001772750"}]
	 */
	 import com.fasterxml.jackson.databind.ObjectMapper;
	@Test
	public void test2() throws Exception {
		Map inputParams = new HashMap();
		List> 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 inputParams = new HashMap();
		Map 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);

	}

你可能感兴趣的:(java中 使用json封装对象集合||数组方式)