SDET json解析与打包
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDET json解析与打包相关的知识,希望对你有一定的参考价值。
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数据
}
以上是关于SDET json解析与打包的主要内容,如果未能解决你的问题,请参考以下文章
解析:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项
rpc框架yar之源码解析- 打包(json, msgpack, serialize)
使用 json rereiver php mysql 在片段中填充列表视图
ios - Heroku 和 MongoDb 上的自定义解析服务器错误 3080:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项