当前位置:网站首页>org.json.JSONObject对象转json,json新增元素,根据json的key获取值。以及list对象格式字符串转jsonArray
org.json.JSONObject对象转json,json新增元素,根据json的key获取值。以及list对象格式字符串转jsonArray
2022-06-30 14:10:00 【全栈程序员站长】
首先设置好多类,并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
+ "]";
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106274.html原文链接:https://javaforall.cn
边栏推荐
- visualstudio 和sql
- Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
- Rpm2rpm packaging steps
- Calculates the length of the last word in a string, separated by spaces
- Three uses of golang underscores
- Unity Animator 参数
- I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
- Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
- Waving flags and shouting for basic language
- Begin End use the pit encountered
猜你喜欢

More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked

损失函数:DIOU loss手写实现

编程实战赛来啦!B站周边、高级会员等好礼送你啦!

Intelligent operation and maintenance: visual management system based on BIM Technology

Jetpack compose for perfect screen fit

Deep understanding Net (2) kernel mode 4 Summary of kernel pattern constructs

可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
![[kali] Kali system, software update (with image source)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[kali] Kali system, software update (with image source)

Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance

深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
随机推荐
Shell programming overview
Why does the folder appear open in another program
DNS 解析之家庭网络接入 Public DNS 实战
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
Go language func function
Talk about Vue's two terminal diff algorithm, analysis of the rendering principle of the mobile terminal, and whether the database primary key must be self incremented? What scenarios do not suggest s
Begin End use the pit encountered
QQ 居然被盗了?原因在这......
Comprehensively analyze the basic features and summary of free and paid SSH tools
随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
Loss function: Diou loss handwriting implementation
Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
Optimization of unit test efficiency: why test programs? What are the benefits of testing?
Yousi College: Six Sigma is not just statistics!
DNS resolution home network access public DNS practice
[Title brushing] coco, who likes bananas
Directory related commands
Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
Cost forecast of PMP (BAC, EAC, etc)