当前位置:网站首页>org. json. The jsonobject object is converted to JSON, and JSON adds new elements. The value is obtained according to the JSON key. And list object format string to jsonarray
org. json. The jsonobject object is converted to JSON, and JSON adds new elements. The value is obtained according to the JSON key. And list object format string to jsonarray
2022-06-30 14:14:00 【Full stack programmer webmaster】
Set up many classes first , and set value , Make a more complex one containing map ,list , Nested objects at the same level
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);
//--------- This completes the assignment of complex objects
// Object turn json
JSONObject jsonObject=new JSONObject(allJava);
System.err.println("========"+jsonObject.toString());
// The output is zero :========{"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 {
// adopt key Values obtained value The first way
Object aaString=jsonObject.get("creditMap");
JSONObject jsonObject2=new JSONObject(aaString.toString());
Object aaString2=jsonObject2.get("creditData");
System.out.println(aaString2);
// The output is zero :{"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());
// Direct output
System.out.println(jsonObject3.get("zzcAntifraudReport_id"));
// The output is :tt
System.out.println(jsonObject3.get("antifraudHittedRulesList"));
// The output is :[{"dd":"dd","cc":"CC"},{"dd":"dd2","cc":"cc2"}]
// Use getString(),getInt() Wait for the built-in method to get the value
String reportId=jsonObject3.getString("zzcAntifraudReport_id");
System.err.println("reportId"+reportId);//reportIdtt
int count=jsonObject3.getInt("zzcAntifraudReport_count");
System.err.println("count"+count);//count33
// Use jsonArray obtain map or list<object> Format string , Turn into 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"}]
// Use getBoolean() obtain boolean type
boolean ifsuccess=jsonObject.getBoolean("ifsuccess");
System.err.println("ifsuccess="+ifsuccess);//ifsuccess=true
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}Class used : From inside to outside :
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
+ "]";
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/106274.html Link to the original text :https://javaforall.cn
边栏推荐
- "As a service", the inevitable choice of enterprise digital transformation
- 我想问一下招商证券怎么开户?通过链接办理股票开户安全吗
- 数据恢复软件EasyRecovery15下载
- Notes on reverse learning in the first week of winter vacation
- Why can't the database table be written into data
- I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
- The programming competition is coming! B station surrounding, senior members and other good gifts to you!
- 半导体动态杂谈
- Unity animator parameter
- Realize a simple LAN communication (similar to feiqiu)
猜你喜欢

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation

单元测试效率优化:为什么要对程序进行测试?测试有什么好处?

Step by step | help you easily submit Google play data security form
![[Title brushing] avoid flooding](/img/2d/95498d54c0c3c5ca79bb72b7a977bc.png)
[Title brushing] avoid flooding
![[observation] as the intelligent industry accelerates, why should AI computing power take the lead?](/img/61/b446a616e86247507c27390505dc6b.jpg)
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?

QQ 居然被盗了?原因在这......

go channel && select

Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)

Wuenda 2022 machine learning special course evaluation is coming!
随机推荐
Detailed explanation of the first three passes of upload Labs
IM即时通讯应用开发中无法解决的“顽疾”
Getting started with shell Basics
Jetpack Compose 实现完美屏幕适配
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
数字化转型道阻且长,如何迈好关键的第一步
Dart 扩展特性
Inexplicable error occurred in unity's frequent switching branch result model
SQL attendance statistics monthly report
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
香港回归20余年,图扑数字孪生港珠澳大桥,超震撼
[Title brushing] coco, who likes bananas
Unity animator parameter
@component使用案例
重磅:国产IDE发布,由阿里研发,完全开源!
Tencent two sides: @bean and @component are used on the same class. What happens?
About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)
目录相关命令
numpy 创建空数组 data = np.empty(shape=[1, 64,64,3])
Race of golang