Java中如何使用Json進行格式轉換常用方法

  • 首先要在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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章