当前位置:网站首页>First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
2022-07-07 01:42:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
1、 The need for jar The parcel json-lib.jar
Download link :
http://sourceforge.net/projects/json-lib/files/json-lib/
This package also requires the following dependent packages ,
commons-lang.jar
commons-beanutils .jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
2、 establish javabean file , Take the following as an example :
<span style="font-size:18px;">package com.gdhdcy.whp;
public class person_bean {
public int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMoblie() {
return moblie;
}
public void setMoblie(String moblie) {
this.moblie = moblie;
}
public String name;
public String sex;
public String moblie;
}
</span>
3、bean/LIst/map Generate json Code for :
<span style="font-size:18px;">package com.gdhdcy.whp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class json_create {
public static void main(String args[]){
//javabean Generate json
person_bean pb=new person_bean();
pb.setId(1);
pb.setName("wang");
pb.setSex(" male ");
pb.setMoblie("1341234568");
JSONObject jsonobject=JSONObject.fromObject(pb);
System.out.println(jsonobject.toString());
// result
//{"id":1,"moblie":"1341234568","name":"wang","sex":" male "}
//List Generate json
person_bean pb1=new person_bean();
pb1.setId(2);
pb1.setName("wang1");
pb1.setSex(" Woman ");
pb1.setMoblie("13123323");
person_bean pb2=new person_bean();
pb2.setId(3);
pb2.setName("wang3");
pb2.setSex(" Woman ");
pb2.setMoblie("13232343673");
List<Object> list=new ArrayList<Object>();
list.add(pb1);
list.add(pb2);
JSONArray jsonarray=JSONArray.fromObject(list);
System.out.println(jsonarray.toString());
/**
* The results are as follows
*/
//[{"id":2,"moblie":"13123323","name":"wang1","sex":" Woman "},{"id":3,"moblie":"13232343673","name":"wang3","sex":" Woman "}]
//map convert to json
HashMap<String, String> map=new HashMap<String, String>();
map.put("1", "hai");
map.put("2", "tian");
map.put("3", "zhi");
JSONObject jsonobject1=JSONObject.fromObject(map);
System.out.println(jsonobject1.toString());
// result :
//{"3":"zhi","2":"tian","1":"hai"}
}
}
</span>
4、 Results of execution :
{“id”:1,”moblie”:”1341234568″,”name”:”wang”,”sex”:” male ”} [{“id”:2,”moblie”:”13123323″,”name”:”wang1″,”sex”:” Woman ”},{“id”:3,”moblie”:”13232343673″,”name”:”wang3″,”sex”:” Woman ”}] {“3″:”zhi”,”2″:”tian”,”1″:”hai”}
Copyright notice : This article is the original article of the blogger , Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116896.html Link to the original text :https://javaforall.cn
边栏推荐
- Make Jar, Not War
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
- 使用nodejs完成判断哪些项目打包+发版
- 各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
- 黑马笔记---创建不可变集合与Stream流
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
- JS ES5也可以创建常量?
- tansig和logsig的差异,为什么BP喜欢用tansig
- LeetCode:1175. Prime permutation
猜你喜欢
域分析工具BloodHound的使用说明
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
Appium foundation - appium inspector positioning tool (I)
Appium自动化测试基础 — uiautomatorviewer定位工具
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Comparison of picture beds of free white whoring
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
2022 Google CTF SEGFAULT LABYRINTH wp
AI automatically generates annotation documents from code
【C语言进阶篇】指针的8道笔试题
随机推荐
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)
交叉验证如何防止过拟合
永久的摇篮
POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
对C语言数组的再认识
AcWing 1142. Busy urban problem solving (minimum spanning tree)
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
Make Jar, Not War
Table table setting fillet
Reptile practice (VI): novel of climbing pen interesting Pavilion
AcWing 345. 牛站 题解(floyd的性质、倍增)
Taro applet enables wxml code compression
Dark horse notes - create immutable sets and streams
Spark TPCDS Data Gen
454-百度面经1
Share a general compilation method of so dynamic library
MySQL最基本的SELECT(查询)语句
分享一个通用的so动态库的编译方法
Use nodejs to determine which projects are packaged + released
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)