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

方式一:已数组的方式封装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);

	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值