1. 程式人生 > >Java中如何使用Json進行格式轉換常用方法

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;
}