欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
	}