Java中如何使用Json进行格式转换常用方法

版权声明:ApassionBoy https://blog.csdn.net/weixin_43150581/article/details/82834356
  • 首先要在pom.xml文件加入以下一依赖,这是阿里巴巴的开源格式转换技术

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.5</version>
</dependency>


  • 以下是前端向后台传的Jsons参数(此参数包含了数组与集合)

packet是一个最大的对象

Requesthead与Body是packet中的小对象(相当packet中的两个属性)

Body中是一个数组形式的包含了这些属性{ }带表一个元素,当然这里是代表对象的值 

 

{
	"Packet": {
		"Requesthead": {
			"uuid": "5b022ba7-8a1b-4089-8078-da2ed1f1494c",
			"clientId": "cxh",
			"orgId": "third:tianxin:13900000789"
		},
		"Body": [{
				"userAccount": "ceshi112",
				"userMobile": "13536788789",
				"userFullname": "定损测试人员",
				"userType": "1",
				"roleNmae": "定损员",
				"orgName": "甜新公司",
				"unitCode": "tx44010000",
				"parentUnitCode": "tx44000000",
				"status": "C",
				"userPassword": "12345"
			},
			{
				"userAccount": "ceshi113",
				"userMobile": "13536788789",
				"userFullname": "协陪人员",
				"userType": "1",
				"roleNmae": "协陪人员",
				"orgName": "甜新公司",
				"unitCode": "tx44010000",
				"parentUnitCode": "tx44000000",
				"status": "C",
				"userPassword": "12345"
			}
		]
	}
}

 

 


  • 后台接收Json字符进行转换和处理 (以下是在接口中将参数装换成对象)
/**
 * @postType 参数类型
 * @input 为参数
 */
@MethodParameter(desc = "synUserList", input = "json,user", postType = {
		String.class }, postName = "json", queryString = "", userParam = "user", httpMethod = "post")
@Override
public Map<String, Object> synUserList(String json, User user) throws Exception {
	//打印出参数
	log.info("请求数据:" + json);
	Map<String, Object> map = new HashMap<String, Object>();
	//将参数转换成大对象
	RequestVo requestVo = JSONObject.parseObject(json, RequestVo.class);
	//大对象将值赋值给小对象
	Packet packet = requestVo.getPacket();
	//调用对内接口,将获取到的值传过去
	map = this.synUserListAdd(packet, user);
	return map;
}
  • 上面的代码调用下面的这个方法,传值过来,完成对象转换List集合
@MethodParameter(desc = "synUserListAdd", input = "packet,user", postType = { List.class,
		UserPojo.class }, postName = "userPojoList", queryString = "", userParam = "user", httpMethod = "post")
@Override
public Map<String, Object> synUserListAdd(Packet packet, User user) throws Exception {
	//用于返回报文
	Map<String, Object> inpumap = new HashMap<String, Object>();
	Map<String, Object> map = new HashMap<String, Object>();
	Map<String, Object> mapres = new HashMap<String, Object>();
	
	UserGroupRole userGroud = null; // 用户组角色表
	List<UserPojo> userPojo = new ArrayList<UserPojo>(); // 对象表
	List<UserPojo> userPojoList = packet.getBody(); //将大对象参数的值赋给对象

	try {
		if (userPojoList != null && userPojoList.size() > 0) {
			mapres.put("uuid", packet.getRequesthead().getUuid());
			mapres.put("responseCode", 1);
			mapres.put("errorMessage", "操作成功");
			map.put("Responsehead", mapres);
			inpumap.put("Packet", map);
			return inpumap;
		} else {
			inpumap.put("success", false);
			inpumap.put("responseCode", "0");
			inpumap.put("errorMessage", "参数为空");
			return inpumap;
		}
	} catch (Exception e) {
		e.printStackTrace();
		mapres.put("uuid", packet.getRequesthead().getUuid());
		mapres.put("responseCode", "0");
		mapres.put("errorMessage", "操作失败");
		map.put("Responsehead", mapres);
		inpumap.put("Packet", map);
	}
	return inpumap;
}

猜你喜欢

转载自blog.csdn.net/weixin_43150581/article/details/82834356
今日推荐