列子如下:
package com.gbt.utils;
import java.util.ArrayList;
import java.util.List;
import com.gbt.model.Accesstoken;
import com.gbt.model.ResponseBoxMessage;
import com.gbt.model.SubmitResultReturn;
import com.gbt.model.TaskPageFileTask;
import com.gbt.model.TaskResultMessage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtil {
@SuppressWarnings("rawtypes")
public static Object jsonString2Object(String jsonString,Class pojoCalss) {
Object pojo;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
pojo = JSONObject.toBean(jsonObject,pojoCalss);
return pojo;
}
public static String object2JsonString(Object javaObj) {
JSONObject json;
json = JSONObject.fromObject(javaObj);
return json.toString();
}
@SuppressWarnings({ "rawtypes","unchecked" })
public static List jsonList2JavaList(String jsonString,Class pojoClass) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
JSONObject jsonObject;
Object pojoValue;
List list = new ArrayList();
for (int i = 0; i < jsonArray.size(); i++) {
jsonObject = jsonArray.getJSONObject(i);
pojoValue = JSONObject.toBean(jsonObject,pojoClass);
list.add(pojoValue);
}
return list;
}
/** * 获取AccessToken对象 * * @param jsonString * @return */
public static Accesstoken getAccessToken(String jsonString) {
Accesstoken token = new Accesstoken();
JSONObject item = JSONObject.fromObject(jsonString);
token.setAccesstoken(item.getString("access_token"));
token.setExpiresin(item.getString("expires_in"));
token.setStatus(item.getString("status"));
token.setInfo(item.getString("info"));
return token;
}
/** * 返回ResponseBoxMessage箱子信息 * * @param jsonString * @return */
public static List<ResponseBoxMessage> getResponseBoxMessage(
String jsonString) {
List<ResponseBoxMessage> Boxlist = new ArrayList<ResponseBoxMessage>();
JSONArray array = JSONArray.fromObject(jsonString);
for (int i = 0; i < array.size(); i++) {
ResponseBoxMessage Box = new ResponseBoxMessage();
// 获取各个属性的值
JSONObject item = array.getJSONObject(i);
Box.setTaskNo(item.getString("taskNo"));
Box.setTaskType(item.getString("taskType"));
Box.setTaskTime(item.getString("taskTime"));
Box.setTaskTimeLimit(item.getInt("taskTimeLimit"));
Box.setCabinetNo(item.getString("cabinetNo"));
Box.setBoxType(item.getString("BoxType"));
Box.setBoxNo(item.getInt("BoxNo"));
Box.setLatticeNo(item.getString("latticeNo"));
Box.setCardNo(item.getString("cardNo"));
Box.setFileNos(item.getString("fileNos"));
Box.setStatus(item.getInt("status"));
Box.setInfo(item.getString("info"));
Boxlist.add(Box);
}
return Boxlist;
}
/** * 返回执行消息 * @param jsonString * @return */
public static TaskResultMessage getTaskResultMessage(String jsonString) {
TaskResultMessage task = new TaskResultMessage();
JSONObject item = JSONObject.fromObject(jsonString);
task.setTaskNo(item.getString("taskNo"));
task.setTaskType(item.getString("taskType"));
task.setTaskResult(item.getString("taskResult"));
task.setTaskTime(item.getString("taskTime"));
task.setCabinetNo(item.getString("cabinetNo"));
task.setBoxType(item.getString("BoxType"));
task.setBoxNo(item.getInt("BoxNo"));
task.setLatticeNo(item.getString("latticeNo"));
task.setCardNo(item.getString("cardNo"));
task.setFileNos(item.getString("fileNos"));
task.setStatus(item.getInt("status"));
task.setInfo(item.getString("info"));
return task;
}
/*** * * @param jsonString * @return */
@SuppressWarnings("unchecked")
public static TaskPageFileTask getTaskPageFileTask(String jsonString){
TaskPageFileTask filetask = new TaskPageFileTask();
JSONObject item = JSONObject.fromObject(jsonString);
filetask.setCurrentPage(item.getString("currentPage"));
filetask.setPageSize(item.getString("pageSize"));
filetask.setTotalSize(item.getString("totalSize"));
filetask.setAccess_token(item.getString("access_token"));
filetask.setFileTasks(item.getJSONArray("fileTasks"));
filetask.setStatus(item.getString("status"));
filetask.setInfo(item.getString("info"));
return filetask;
}
/** * 提交任务返回结果 * @param jsonString * @return */
public static SubmitResultReturn getSubmitResultReturn(String jsonString){
SubmitResultReturn sub = new SubmitResultReturn();
JSONObject item = JSONObject.fromObject(jsonString);
sub.setAccesstoken(item.getString("access_token"));
sub.setStatus(item.getString("status"));
sub.setInfo(item.getString("info"));
return sub;
}
}