利用JSONObject将下面这个json字符串转换为javabean对象
{
"assetInfoList":[{
"devsno":"xxx",
"assetno":"xxx",
"domain":"xxx",
"devtype":"xxx",
"category":"xxx",
"vendor":"xxx",
"hardver":"xxx",
"softwarever":"xxx",
"custname":"xxx",
"custaddr":"xxx!",
"contactinfo":"xxx",
"orders":"xxx,xxx",
"services":"xxx,xxx,xxx",
"opertime":"xxx",
"orderNumber":"xxx",
"taskNumber":"xxx",
"multicast":"xxx"}],
"infname":"xxx",
"requestserial":"xxx",
"sysname":"xxx"
}
解析过程分析:
private AMSRequestStruct decode(String jsonStr) {
//实体对象
AMSRequestStruct request = new AMSRequestStruct();
//将jsonStr转为JSON对象
JSONObject obj = JSONObject.fromObject(jsonStr);
//获取json对象的值 json格式为key-value形式
request.setSysname(obj.getString("sysname").trim());
request.setRequestserial(obj.getString("requestserial").trim());
request.setInfname(obj.getString("infname").trim());
//json数组转换为JSONArray
JSONArray array = obj.getJSONArray("assetInfoList");
//声明一个list
ArrayList list = new ArrayList();
//循环遍历json数组
for (int i = 0; i < array.size(); i++) {
JSONObject asset = array.getJSONObject(i);
AssetInfo info = new AssetInfo();
info.setDevsno(asset.getString("devsno").trim());
info.setAssetno(asset.getString("assetno").trim());
info.setCmsdomain(asset.getString("domain").trim());
info.setDomain(asset.getString("domain").trim());
info.setDevtype(asset.getString("devtype").trim());
info.setCategory(asset.getString("category").trim());
info.setVendor(asset.getString("vendor").trim());
info.setHardver(asset.getString("hardver").trim());
info.setSoftwarever(asset.getString("softwarever").trim());
info.setCustname(asset.getString("custname").trim());
info.setCustaddr(asset.getString("custaddr").trim());
info.setContactinfo(asset.getString("contactinfo").trim());
info.setOrders(asset.getString("orders").trim());
info.setServices(asset.getString("services").trim());
info.setMulticast(asset.getString("multicast").trim());
info.setOpertime(asset.getString("opertime").trim());
info.setStatus(request.getInfname2(request.getInfname()));
info.setDevstatus(request.getFlag2(request.getInfname2(request.getInfname())));
info.setInvtype(request.getFlag3(request.getInfname2(request.getInfname())));
info.setOrdernumber(asset.getString("orderNumber"));
info.setTasknumber(asset.getString("taskNumber"));
//将info添加到list中
list.add(info);
}
request.setAssetInfoList(list);
return request;
}
如果觉得《java bean jsonobject_利用JSONObject将json 字符串转换为java bean对象》对你有帮助,请点赞、收藏,并留下你的观点哦!