1. 程式人生 > >Java常用型別轉換寫法

Java常用型別轉換寫法

1. json轉字串

String requestStr = JSON.toJSONString(requestObj);

2. json轉物件

String responseStr =  HttpHelper.post(targetUrl, requestStr, token);
Object[] responseObj = JSON.parseObject(responseStr, Object[].class);

3. 一個物件成員變數值複製到另一物件中:

使用package org.springframework.beans;包下的BeanUtils.copyProperties(source, target);

即可

BeanUtils.copyProperties(request, MDevice);

複製的資料在request中,使用時用.訪問

4. 字串轉json:

String responseStr =  HttpHelper.post(url, requestStr, token);
JSONObject responseJson = (JSONObject) JSON.parse(responseStr);