part 1 建立DataStream类
package com.huaqin.hat;
/**
* creator:perfe wang
* time:2016/6/28
* description:数据类,特用于json解析和打包,属性为json对象的key,属性值为json的内部类
* 其内部类用于规定其属性的传递格式
*/
import java.util.ArrayList;
import java.util.List;
publicclassDataStream{
publicint TYPE =-1;
publicList<Device> devices_list =newArrayList<Device>();
publicList<CaseSend> case_send_list =newArrayList<CaseSend>();
publicList<CaseReceive> case_receive_list =newArrayList<CaseReceive>();
publicstaticclassDevice{
publicint id =-1;
publicString name ="";
publicString sim1 ="";
publicString sim2 ="";
publicint sdcard =-1;
publicint state =-1;
publicint waiting_case_count =-1;
publicString configversion ="";
publicint deviceId =-1;
}
publicstaticclassCaseSend{
publicString name ="";
publicint type =-1;
publiclong jarId =-1;
publiclong caseId =-1;
publicString address ="";
publicint caseStastusId =-1;
publicint planId =-1;
publicString sim1 ="";
publicString sim2 ="";
publicint sdcard =-1;
}
publicstaticclassCaseReceive{
publicString case_stastus ="";
publicString logAdrr ="";
publicint caseStastusId =-1;
publicint planId =-1;
publicString startTime ="";
publicString endTime ="";
}
}
part 2 建立json解析工具
package com.huaqin.hat;
/**
* creator:perfe wang
* time:2016/6/28
* description:基于DataStream的json解析工具类
* receive方法接受json对象解析成DataStream对象并返回
* send方法接受DataStream对象打包成json字符串并返回
* caseJsonUtil接受原始的DbAdapter.Case.Object对象解析成CaseSend对象并返回
*/
import net.sf.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huaqin.hat.DataStream.CaseSend;
publicclassDataJsonUtil{
publicDataStream receive(String data){
Gson gson =newGson();
java.lang.reflect.Type type =newTypeToken<DataStream>(){
}.getType();
DataStream data_receive = gson.fromJson(data, type);
return data_receive;
}
publicString send(DataStream dataBean){
Gson gson =newGson();
String json = gson.toJson(dataBean);
return json;
}
publicCaseSend caseJsonUtil(DbAdapter.Case.Object caseObject){
CaseSend caseSend =newCaseSend();
caseSend.name = caseObject.name;
caseSend.type = caseObject.type;
caseSend.address = caseObject.address;
caseSend.jarId = caseObject.jarId;
caseSend.caseId = caseObject.caseId;
String json = caseObject.description;
JSONObject jsonObject =JSONObject.fromObject(json);
caseSend.sdcard = jsonObject.getInt("sdcard");
caseSend.sim1 = jsonObject.getString("sim1");
caseSend.sim2 = jsonObject.getString("sim2");
return caseSend;
}
}
part 3 自己实现自测试
package com.huaqin.hat;
import java.util.List;
import com.huaqin.hat.DataStream.Device;
publicclassJsonTest{
/**
* @param args
*/
publicstaticvoid main(String[] args){
// json工具类
DataJsonUtil jsonUtil =newDataJsonUtil();
// 接受的data数据
// 发送的data数据
}
时间: 2024-12-08 18:34:19