當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSON转换为对象
第一種,直接轉換對象實體(對象不包含對象的)
所用的包:
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;}?
總結
- 上一篇: 签名验签自助排查
- 下一篇: Spring Boot之HelloWor