首先设置好多类,并set值,制作一个比较复杂的含有map ,list ,等层级嵌套的对象
package testMap.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import testMap.bean.AllJava;
import testMap.bean.CreditData;
import testMap.bean.ZzcAntifraudReport_antifraudHittedRulesList;
import testMap.bean.ZzcAntifraudReport_blacklistRecordsList;
import testMap.bean.ZzcAntifraudReport_reasonCode;
public class TestClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ZzcAntifraudReport_antifraudHittedRulesList rule=new ZzcAntifraudReport_antifraudHittedRulesList();
rule.setCc("CC");
rule.setDd("dd");
ZzcAntifraudReport_antifraudHittedRulesList rule2=new ZzcAntifraudReport_antifraudHittedRulesList();
rule2.setCc("cc2");
rule2.setDd("dd2");
ZzcAntifraudReport_blacklistRecordsList records=new ZzcAntifraudReport_blacklistRecordsList();
records.setAa("aa");
records.setBb("bb");
ZzcAntifraudReport_reasonCode code=new ZzcAntifraudReport_reasonCode();
code.setEe("ee");
code.setFf("ff");
CreditData data=new CreditData();
List rulesLists=new ArrayList();
rulesLists.add(rule);
rulesLists.add(rule2);
data.setAntifraudHittedRulesList(rulesLists);
data.setZzcAntifraudReport_count(33);
data.setZzcAntifraudReport_id("tt");
AllJava allJava=new AllJava();
Map creditMapsMap=new HashMap();
creditMapsMap.put("creditData", data);
allJava.setCreditMap(creditMapsMap);
allJava.setIfsuccess(true);
//---------至此为复杂对象赋值完毕
//对象转json
JSONObject jsonObject=new JSONObject(allJava);
System.err.println("========"+jsonObject.toString());
// 输出的结果是:========{"ifsuccess":true,"creditMap":{"creditData":{"zzcAntifraudReport_executedAt":null,"resonCode":null,"zzcAntifraudReport_id":"tt","blacklistRecordsList":null,"zzcAntifraudReport_count":33,"zzcAntifraudReport_riskLevel":null,"antifraudHittedRulesList":[{"dd":"dd","cc":"CC"},{"dd":"dd2","cc":"cc2"}],"zzcAntifraudReport_tenantCount":0}}}
try {
//通过key值获取value的第一种方法
Object aaString=jsonObject.get("creditMap");
JSONObject jsonObject2=new JSONObject(aaString.toString());
Object aaString2=jsonObject2.get("creditData");
System.out.println(aaString2);
// 输出的结果是:{"zzcAntifraudReport_executedAt":null,"resonCode":null,"zzcAntifraudReport_id":"tt","blacklistRecordsList":null,"zzcAntifraudReport_count":33,"antifraudHittedRulesList":[{"dd":"dd","cc":"CC"},{"dd":"dd2","cc":"cc2"}],"zzcAntifraudReport_riskLevel":null,"zzcAntifraudReport_tenantCount":0}
JSONObject jsonObject3=new JSONObject(aaString2.toString());
//直接输出
System.out.println(jsonObject3.get("zzcAntifraudReport_id"));
// 输出结果是:tt
System.out.println(jsonObject3.get("antifraudHittedRulesList"));
// 输出结果是:[{"dd":"dd","cc":"CC"},{"dd":"dd2","cc":"cc2"}]
// 使用getString(),getInt()等自带方法获取值
String reportId=jsonObject3.getString("zzcAntifraudReport_id");
System.err.println("reportId"+reportId);//reportIdtt
int count=jsonObject3.getInt("zzcAntifraudReport_count");
System.err.println("count"+count);//count33
// 使用jsonArray 获取map或list
使用到的类:从里到外:
package testMap.bean;
public class ZzcAntifraudReport_reasonCode {
String ee;
String ff;
public String getEe() {
return ee;
}
public void setEe(String ee) {
this.ee = ee;
}
public String getFf() {
return ff;
}
public void setFf(String ff) {
this.ff = ff;
}
}
package testMap.bean;
public class ZzcAntifraudReport_blacklistRecordsList {
String aa;
String bb;
public String getAa() {
return aa;
}
public void setAa(String aa) {
this.aa = aa;
}
public String getBb() {
return bb;
}
public void setBb(String bb) {
this.bb = bb;
}
}
package testMap.bean;
public class ZzcAntifraudReport_antifraudHittedRulesList {
String cc;
String dd;
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
@Override
public String toString() {
return "ZzcAntifraudReport_antifraudHittedRulesList [cc=" + cc
+ ", dd=" + dd + "]";
}
}
package testMap.bean;
import java.util.Date;
import java.util.List;
public class CreditData {
private String ZzcAntifraudReport_id;
private Date ZzcAntifraudReport_executedAt;
private List blacklistRecordsList;
private List antifraudHittedRulesList;
private List resonCode;
private int ZzcAntifraudReport_tenantCount;
private String ZzcAntifraudReport_riskLevel;
private int ZzcAntifraudReport_count;
public String getZzcAntifraudReport_id() {
return ZzcAntifraudReport_id;
}
public void setZzcAntifraudReport_id(String zzcAntifraudReport_id) {
ZzcAntifraudReport_id = zzcAntifraudReport_id;
}
public Date getZzcAntifraudReport_executedAt() {
return ZzcAntifraudReport_executedAt;
}
public void setZzcAntifraudReport_executedAt(Date zzcAntifraudReport_executedAt) {
ZzcAntifraudReport_executedAt = zzcAntifraudReport_executedAt;
}
public List getBlacklistRecordsList() {
return blacklistRecordsList;
}
public void setBlacklistRecordsList(
List blacklistRecordsList) {
this.blacklistRecordsList = blacklistRecordsList;
}
public List getAntifraudHittedRulesList() {
return antifraudHittedRulesList;
}
public void setAntifraudHittedRulesList(
List antifraudHittedRulesList) {
this.antifraudHittedRulesList = antifraudHittedRulesList;
}
public List getResonCode() {
return resonCode;
}
public void setResonCode(List resonCode) {
this.resonCode = resonCode;
}
public int getZzcAntifraudReport_tenantCount() {
return ZzcAntifraudReport_tenantCount;
}
public void setZzcAntifraudReport_tenantCount(int zzcAntifraudReport_tenantCount) {
ZzcAntifraudReport_tenantCount = zzcAntifraudReport_tenantCount;
}
public String getZzcAntifraudReport_riskLevel() {
return ZzcAntifraudReport_riskLevel;
}
public void setZzcAntifraudReport_riskLevel(String zzcAntifraudReport_riskLevel) {
ZzcAntifraudReport_riskLevel = zzcAntifraudReport_riskLevel;
}
public int getZzcAntifraudReport_count() {
return ZzcAntifraudReport_count;
}
public void setZzcAntifraudReport_count(int zzcAntifraudReport_count) {
ZzcAntifraudReport_count = zzcAntifraudReport_count;
}
@Override
public String toString() {
return "CreditData [ZzcAntifraudReport_id=" + ZzcAntifraudReport_id
+ ", ZzcAntifraudReport_executedAt="
+ ZzcAntifraudReport_executedAt + ", blacklistRecordsList="
+ blacklistRecordsList + ", antifraudHittedRulesList="
+ antifraudHittedRulesList + ", resonCode=" + resonCode
+ ", ZzcAntifraudReport_tenantCount="
+ ZzcAntifraudReport_tenantCount
+ ", ZzcAntifraudReport_riskLevel="
+ ZzcAntifraudReport_riskLevel + ", ZzcAntifraudReport_count="
+ ZzcAntifraudReport_count + "]";
}
}
package testMap.bean;
import java.util.Map;
public class AllJava {
boolean ifsuccess;
Map creditMap;
public boolean isIfsuccess() {
return ifsuccess;
}
public void setIfsuccess(boolean ifsuccess) {
this.ifsuccess = ifsuccess;
}
public Map getCreditMap() {
return creditMap;
}
public void setCreditMap(Map creditMap) {
this.creditMap = creditMap;
}
@Override
public String toString() {
return "AllJava [ifsuccess=" + ifsuccess + ", creditMap=" + creditMap
+ "]";
}
}