JSON转换为对象
程序员文章站
2024-03-18 18:20:04
...
第一种,直接转换对象实体(对象不包含对象的)
所用的包:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
转换:
List<Tbempcdbdksqbr> sqbrList=new ArrayList<Tbempcdbdksqbr>();
if(StringUtils.isNotBlank(json)) {
System.out.println("JSON解析-2--");
JSONArray jsonAyyay2 = JSONObject.parseArray(json);
//JSONArray jsonAyyay = JSONObject.parseObject(jsonAyyay2).getJSONArray("dataList");
sqbrList = JSONArray.parseArray(jsonAyyay2.toJSONString(),Tbempcdbdksqbr.class);
}
第二种,对象里面包含对象的
A.对象集合
package com.chinauip.cfc.business.insurancebureau.ylbx.entity;
import java.util.ArrayList;
import java.util.List;
import com.chinauip.cfc.run.common.json.JsonUtil;
public class Statements {
private List<StatementsBsMediFee> bsMediFee;
private List<StatementsPreFeeValue> preFeeValue;
private List<StatementsSquareFund> squareFund;
private List<StatementsSquareInfo> squareInfo;
private List<StatementsDataFund> dataFund;
private List<StatementsFeeValue> feeValue;
private List<StatementsPreFee> preFee;
private List<StatementsSquareFee> squareFee;
public List<StatementsBsMediFee> getBsMediFee() {
return bsMediFee;
}
public void setBsMediFee(List<StatementsBsMediFee> bsMediFee) {
this.bsMediFee = bsMediFee;
}
public List<StatementsPreFeeValue> getPreFeeValue() {
return preFeeValue;
}
public void setPreFeeValue(List<StatementsPreFeeValue> preFeeValue) {
this.preFeeValue = preFeeValue;
}
public List<StatementsSquareFund> getSquareFund() {
return squareFund;
}
public void setSquareFund(List<StatementsSquareFund> squareFund) {
this.squareFund = squareFund;
}
public List<StatementsSquareInfo> getSquareInfo() {
return squareInfo;
}
public void setSquareInfo(List<StatementsSquareInfo> squareInfo) {
this.squareInfo = squareInfo;
}
public List<StatementsDataFund> getDataFund() {
return dataFund;
}
public void setDataFund(List<StatementsDataFund> dataFund) {
this.dataFund = dataFund;
}
public List<StatementsFeeValue> getFeeValue() {
return feeValue;
}
public void setFeeValue(List<StatementsFeeValue> feeValue) {
this.feeValue = feeValue;
}
public List<StatementsPreFee> getPreFee() {
return preFee;
}
public void setPreFee(List<StatementsPreFee> preFee) {
this.preFee = preFee;
}
public List<StatementsSquareFee> getSquareFee() {
return squareFee;
}
public void setSquareFee(List<StatementsSquareFee> squareFee) {
this.squareFee = squareFee;
}
}
B.转换:obj实体
@SuppressWarnings("unchecked")
public Statements getStatementService(Tbinsbjsdhis entity) {
List<Statements> list = new ArrayList<Statements>();
Statements obj = null;
WbxtCommonWSEntity wsEntity = commonWSService.executeService(
"com.chinauip.cfc.business.insurancebureau.ylbx.wsserver.StatementsWSService",
"createStateWSService", entity);
if (wsEntity != null) {
String json = wsEntity.getJson();
if(StringUtils.isNotBlank(json)) {
JSONObject jobj = JSONObject.fromObject(json);
Map classMap = new HashMap();
classMap.put("bsMediFee", StatementsBsMediFee.class);
classMap.put("preFeeValue", StatementsPreFeeValue.class);
classMap.put("squareFund", StatementsSquareFund.class);
classMap.put("squareInfo", StatementsSquareInfo.class);
classMap.put("dataFund", StatementsDataFund.class);
classMap.put("feeValue", StatementsFeeValue.class);
classMap.put("preFee", StatementsPreFee.class);
classMap.put("squareFee", StatementsSquareFee.class);
obj=(Statements) JSONObject.toBean(jobj, Statements.class, classMap);
}
}
return obj;
}
上一篇: 关于c++16进制字符串与ASCII字符串的相互转换
下一篇: 将map转换成json字符串
推荐阅读
-
json转换为对象
-
转换Json对象
-
JSON转换为对象
-
通过HttpServletRequest转换获得json对象 博客分类: springmvc HttpServletRequestjson
-
通过codehaus来实现json写入文件和读取文件成json对象 博客分类: javajson jsonjacksoncodehaus对象java
-
<二> Google Gson 转换Json字符串和对象(日期,枚举的转化) 博客分类: JSON Gson枚举日期内嵌对象
-
springMvc直接自动转化对象为json的配置(不需要自己转成json) 博客分类: springmvc springMvc
-
(转)如何将Sklearn数据集Brunch格式转换为Pandas数据集DataFrame?
-
js将form表单序列化[json字符串、数组、对象] 博客分类: Web-JS/JQuery
-
js将form表单序列化[json字符串、数组、对象] 博客分类: Web-JS/JQuery