当前位置:网站首页>json学习初体验–第三者jar包实现bean、List、map创json格式
json学习初体验–第三者jar包实现bean、List、map创json格式
2022-07-06 17:56:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
1、的需要jar包裹json-lib.jar
下载链接:
http://sourceforge.net/projects/json-lib/files/json-lib/
此包还须要下面的依赖包,
commons-lang.jar
commons-beanutils .jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
2、创建javabean文件,以以下的为例:
<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生成json的代码:
<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生成json
person_bean pb=new person_bean();
pb.setId(1);
pb.setName("wang");
pb.setSex("男");
pb.setMoblie("1341234568");
JSONObject jsonobject=JSONObject.fromObject(pb);
System.out.println(jsonobject.toString());
//结果
//{"id":1,"moblie":"1341234568","name":"wang","sex":"男"}
//List生成json
person_bean pb1=new person_bean();
pb1.setId(2);
pb1.setName("wang1");
pb1.setSex("女");
pb1.setMoblie("13123323");
person_bean pb2=new person_bean();
pb2.setId(3);
pb2.setName("wang3");
pb2.setSex("女");
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());
/**
* 结果例如以下
*/
//[{"id":2,"moblie":"13123323","name":"wang1","sex":"女"},{"id":3,"moblie":"13232343673","name":"wang3","sex":"女"}]
//map转换成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());
//结果:
//{"3":"zhi","2":"tian","1":"hai"}
}
}
</span>
4、执行的结果:
{“id”:1,”moblie”:”1341234568″,”name”:”wang”,”sex”:”男”} [{“id”:2,”moblie”:”13123323″,”name”:”wang1″,”sex”:”女”},{“id”:3,”moblie”:”13232343673″,”name”:”wang3″,”sex”:”女”}] {“3″:”zhi”,”2″:”tian”,”1″:”hai”}
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116896.html原文链接:https://javaforall.cn
边栏推荐
- Gazebo的安装&与ROS的连接
- NEON优化:关于交叉存取与反向交叉存取
- LLDP兼容CDP功能配置
- 让我们,从头到尾,通透网络I/O模型
- [hfctf2020]babyupload session parsing engine
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- MySQL script batch queries all tables containing specified field types in the database
- Dark horse notes - exception handling
- Implementation principle of waitgroup in golang
- JTAG debugging experience of arm bare board debugging
猜你喜欢
如何管理分布式团队?
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
ARM裸板调试之JTAG原理
[case sharing] basic function configuration of network loop detection
Instructions for using the domain analysis tool bloodhound
ARM裸板调试之JTAG调试体验
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
阿里云中mysql数据库被攻击了,最终数据找回来了
LeetCode:1175. 质数排列
Yunna | work order management software, work order management software app
随机推荐
Realize incremental data synchronization between MySQL and ES
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Google发布安全更新,修复Chrome中已被利用的0 day
Dark horse notes - exception handling
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
golang中的atomic,以及CAS操作
Vocabulary in Data Book
Make Jar, Not War
Amway wave C2 tools
让我们,从头到尾,通透网络I/O模型
swiper组件中使用video导致全屏错位
JTAG principle of arm bare board debugging
自旋与sleep的区别
Transplant DAC chip mcp4725 to nuc980
黑马笔记---异常处理
云呐|工单管理软件,工单管理软件APP
Install Firefox browser on raspberry pie /arm device
c语言—数组
Metauniverse urban legend 02: metaphor of the number one player
NEON优化:关于交叉存取与反向交叉存取