首先设置好多类,并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<ZzcAntifraudReport_antifraudHittedRulesList> rulesLists=new ArrayList<ZzcAntifraudReport_antifraudHittedRulesList>();
rulesLists.add(rule);
rulesLists.add(rule2);
data.setAntifraudHittedRulesList(rulesLists);
data.setZzcAntifraudReport_count(33);
data.setZzcAntifraudReport_id("tt");
AllJava allJava=new AllJava();
Map<String, Object> creditMapsMap=new HashMap<String, Object>();
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<object>格式的字符串,转成jsonArray
Object list=jsonObject3.get("antifraudHittedRulesList");
JSONArray jsonArray =jsonObject3.getJSONArray("antifraudHittedRulesList");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject4=jsonArray.getJSONObject(i);
jsonObject4.get("dd");
jsonObject4.get("cc");
System.out.println("---"+jsonObject4.toString());
// ---{"dd":"dd","cc":"CC"}
// ---{"dd":"dd2","cc":"cc2"}
}
System.out.println(jsonArray);
// [{"dd":"dd","cc":"CC"},{"dd":"dd2","cc":"cc2"}]
// 使用getBoolean()获取boolean类型
boolean ifsuccess=jsonObject.getBoolean("ifsuccess");
System.err.println("ifsuccess="+ifsuccess);//ifsuccess=true
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用到的类:从里到外:
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<ZzcAntifraudReport_blacklistRecordsList> blacklistRecordsList;
private List<ZzcAntifraudReport_antifraudHittedRulesList> antifraudHittedRulesList;
private List<ZzcAntifraudReport_reasonCode> 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<ZzcAntifraudReport_blacklistRecordsList> getBlacklistRecordsList() {
return blacklistRecordsList;
}
public void setBlacklistRecordsList(
List<ZzcAntifraudReport_blacklistRecordsList> blacklistRecordsList) {
this.blacklistRecordsList = blacklistRecordsList;
}
public List<ZzcAntifraudReport_antifraudHittedRulesList> getAntifraudHittedRulesList() {
return antifraudHittedRulesList;
}
public void setAntifraudHittedRulesList(
List<ZzcAntifraudReport_antifraudHittedRulesList> antifraudHittedRulesList) {
this.antifraudHittedRulesList = antifraudHittedRulesList;
}
public List<ZzcAntifraudReport_reasonCode> getResonCode() {
return resonCode;
}
public void setResonCode(List<ZzcAntifraudReport_reasonCode> 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<String, Object> creditMap;
public boolean isIfsuccess() {
return ifsuccess;
}
public void setIfsuccess(boolean ifsuccess) {
this.ifsuccess = ifsuccess;
}
public Map<String, Object> getCreditMap() {
return creditMap;
}
public void setCreditMap(Map<String, Object> creditMap) {
this.creditMap = creditMap;
}
@Override
public String toString() {
return "AllJava [ifsuccess=" + ifsuccess + ", creditMap=" + creditMap
+ "]";
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106274.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...