Json字符串转对象和转List集合操作(json-lib版本)

Json是当前开发用得最多基于JavaScript语言的轻量级的数据交换格式,总结一下常用转换格式的方法,以便日后使用方便

以下为 json-lib包各种 JSON 转换的方法总结:

1. 所需引入的第三方包:

                <dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.3</version></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>xom</groupId><artifactId>xom</artifactId><version>1.2.5</version></dependency><dependency><groupId>net.sf.ezmorph</groupId><artifactId>ezmorph</artifactId><version>1.0.4</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk13</classifier></dependency>

2. 对象User类

package com.lmx.demo;public class User
{private String cId;private String uName;private String pwd;public  User(){}public  User(String cId, String uName, String pwd){this.cId = cId;this.uName = uName;this.pwd = pwd;}public String getcId(){return cId;}public void setcId(String cId){this.cId = cId;}public String getuName(){return uName;}public void setuName(String uName){this.uName = uName;}public String getPwd(){return pwd;}public void setPwd(String pwd){this.pwd = pwd;}}

3. Json各类转换

package com.lmx.demo;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.xml.XMLSerializer;/*** json-lib* * @author LMX*/
public class SfJson
{/*** 1. json-lib 的 JSONObject 基础操作*/public void showJSONObject(){// 创建JSONObjectJSONObject jsonObject = new JSONObject();jsonObject.put("uName", "lmx");jsonObject.put("cId", "100");System.out.println(jsonObject);// 输出:{"uName":"lmx","cId":"100"}// 增加属性jsonObject.element("pwd", "123456");System.out.println(jsonObject);// 输出: {"uName":"lmx","cId":"100","pwd":"123456"}// 取值System.out.println("cId:" + jsonObject.get("cId"));// 输出: cId:100// 判读输出对象的类型boolean isArray = jsonObject.isArray();boolean isEmpty = jsonObject.isEmpty();boolean isNullObject = jsonObject.isNullObject();System.out.println("是否数组:" + isArray);// 输出: 是否数组:falseSystem.out.println("是否空:" + isEmpty);// 输出: 是否空:falseSystem.out.println("是否空对象:" + isNullObject);// 输出: 是否空对象:false}/*** 2. json-lib 的 JSONArray 添加到JSONObject 基础操作*/public void showJSONArrayAddJSONObject(){JSONObject jsonObject = new JSONObject();JSONArray jsonArray = new JSONArray();jsonArray.add(0, "lmx");jsonArray.add(1, "jqy");jsonObject.element("uName", jsonArray);System.out.println(jsonObject);// 输出: {"uName":["lmx","jqy"]}}/*** 3. json-lib 的 JSONArray 基础操作* * @param jsonStr*/public void showJSONArray(){// 创建JSONArrayJSONArray jsonArray = new JSONArray();jsonArray.add(0, "lmx");jsonArray.add(1, "jqy");jsonArray.element("llh");System.out.println(jsonArray);// 输出: ["lmx","jqy","llh"]// 根据下标返回,打印:2System.out.println(jsonArray.get(0));// 输出: lmx// set修改jsonArray.set(0, "aa");System.out.println(jsonArray);// 输出: ["aa","jqy","llh"]// 添加最前jsonArray.add(0, "bb");// 添加最后jsonArray.add("cc");System.out.println(jsonArray);// 输出: ["bb","aa","jqy","llh","cc"]// jsonArray.clear(); 清空}/*** 4. json-lib 把JSONObject放入到JSONArray*/public void showJSONObjectAddJSONArray(){JSONObject jsonObject = new JSONObject();jsonObject.put("uName", "lmx");jsonObject.put("pwd", "123456");JSONObject jsonObject2 = new JSONObject();jsonObject2.put("uName", "lmx");jsonObject2.put("pwd", "123456");JSONArray jsonArray = new JSONArray();jsonArray.add(jsonObject);jsonArray.add(jsonObject2);System.out.println(jsonArray);// 输出:[{"uName":"lmx","pwd":"123456"},{"uName":"lmx","pwd":"123456"}]for (int i = 0; i < jsonArray.size(); i++ ){System.out.println(jsonArray.get(i));// 输出: 1. {"uName":"lmx","pwd":"123456"} 2. {"uName":"lmx","pwd":"123456"}}}/*** 5.json-lib 把 JavaBean转换成json字符串*/public void getJSONObjectToBean(){User user = new User();user.setcId("100");user.setuName("lmx");user.setPwd("123456");JSONObject jsonObject = JSONObject.fromObject(user);System.out.println(jsonObject);// 输出: {"uName":"lmx","pwd":"123456","cId":"100"}}/*** 6.json-lib 把 json字符串转换成JavaBean*/public void getBeanToJSONObject(){String strJon = "{\"uName\":\"lmx\",\"pwd\":\"123456\",\"cId\":\"100\"}";JSONObject jsonObject = JSONObject.fromObject(strJon);User user = (User)JSONObject.toBean(jsonObject, User.class);System.out.println(user);// 输出: {"uName":"lmx","pwd":"123456","cId":"100"}}/*** 7. json-lib 把 List转换成json字符串*/public void getListToJSONArray(){// List转json字符串List<User> list = new ArrayList<User>();list.add(new User("100", "lmx", "123456"));list.add(new User("200", "jqy", "123"));JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);// 输出: [{"uName":"lmx","pwd":"123456","cId":"100"},{"uName":"jqy","pwd":"123","cId":"200"}]}/*** 8. json-lib 把 json字符串 转换成 List*/public void getJSONObjectToList(){List<User> userList = new ArrayList<User>();String strJon = "[{\"cId\":\"100\",\"uName\":\"lmx\",\"pwd\":\"123\"},{\"cId\":\"200\",\"uName\":\"jqy\",\"pwd\":\"123456\"}]";JSONArray jsonArray = JSONArray.fromObject(strJon);for (int i = 0; i < jsonArray.size(); i++ ){JSONObject jsonObject = jsonArray.getJSONObject(i);User user = (User)JSONObject.toBean(jsonObject, User.class);userList.add(user);}for (User user : userList){System.out.println(user.getuName());// 输出: lmx jqy}}/*** 9. json-lib 把 Map 转换成 json字符串*/public void getMapToJson(){Map<String, User> map = new HashMap<String, User>();map.put("1", new User("100", "lmx", "123456"));map.put("2", new User("200", "jqy", "123"));JSONObject jsonMap = JSONObject.fromObject(map);System.out.println(jsonMap);// 输出:// {"1":{"uName":"lmx","pwd":"123456","cId":"100"},"2":{"uName":"jqy","pwd":"123","cId":"200"}}}/*** 10. json-lib 把 json字符串转换成Map*/public void getJsonToMap(){// json字符串转MapString strJon = "{\"1\":{\"cId\":\"100\",\"uName\":\"lmx\"},\"2\":{\"cId\":\"200\",\"uName\":\"jqy\"}}";Map map = (Map)JSONObject.fromObject(strJon);Set set = map.keySet();Iterator ite = set.iterator();while (ite.hasNext()){String key = (String)ite.next();JSONObject jsonObject = JSONObject.fromObject(map.get(key));User user = (User)JSONObject.toBean(jsonObject, User.class);System.out.println(key + " " + JSONObject.fromObject(user).toString());// 输出: 1 {"uName":"lmx","pwd":"","cId":"100"} 2{"uName":"jqy","pwd":"","cId":"200"}}}/*** 11. json-lib 把 JSONArray转换成 List*/public void getJSONArrayToList(){// List转型JSONArrayList<User> list = new ArrayList<User>();list.add(new User("100", "lmx", "123456"));list.add(new User("200", "jqy", "123"));JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray.toString());// 输出:[{"uName":"lmx","pwd":"123456","cId":"100"},{"uName":"jqy","pwd":"123","cId":"200"}]// JSONArray转型ListList<User> lists = JSONArray.toList(jsonArray, new User(), new JsonConfig());Iterator<User> iur = lists.iterator();while (iur.hasNext()){User user = iur.next();System.out.println(JSONObject.fromObject(user).toString());// 输出:{"uName":"lmx","pwd":"123456","cId":"100"}// {"uName":"jqy","pwd":"123","cId":"200"}}}/*** 12. json-lib 把JSONArray转换成 数组*/public void getJSONArrayToArray(){boolean[] boolArray = new boolean[] {true, false, true};JSONArray jsonArray = JSONArray.fromObject(boolArray);Object obj[] = jsonArray.toArray();for (Object o : obj){System.out.println(o + " ");// 输出: true false true}}/*** 13. json-lib 把数组转换成 JSONArray*/public void getArrayToJSONArray(){boolean[] boolArray = new boolean[] {true, false, true};JSONArray jsonArray = JSONArray.fromObject(boolArray);System.out.println(jsonArray.toString());// 输出: [true,false,true]}/*** 14. json-lib 把XML转换成 JSON*/public void getXmlToJSON(){// XML转JSONString xml = "<root>" + "<uName>lmx</uName>" + "<pwd>123456</pwd>" + "<birthday>"+ "<year>1990</year>" + "<month>10</month>" + "<day>22</day>" + "</birthday>"+ "</root>";XMLSerializer xmlSerializer = new XMLSerializer();JSON json = xmlSerializer.read(xml);System.out.println(json.toString());// 输出:{"uName":"lmx","pwd":"123456","birthday":{"year":"1990","month":"10","day":"22"}}}/*** 14. json-lib 把JSON转换成 XML*/public void getJSONToXml(){String jsondata = "{\"uName\":\"lmx\",\"pwd\":\"123456\",\"birthday\":{\"year\":\"1990\",\"month\":\"10\",\"day\":\"22\"}}";  JSONObject jsonObject = JSONObject.fromObject(jsondata);  String xmlstr = new XMLSerializer().write(jsonObject);  System.out.println(xmlstr); // 输出: <?xml version="1.0" encoding="UTF-8"?><o><birthday class="object"><day type="string">22</day><month type="string">10</month><year type="string">1990</year></birthday><pwd type="string">123456</pwd><uName type="string">lmx</uName></o>}public static void main(String[] args){// TODO Auto-generated method stubSfJson sf = new SfJson();// 1System.out.println("showJsonObject:");sf.showJSONObject();// 2System.out.println("showJSONArrayAddJSONObject:");sf.showJSONArrayAddJSONObject();// 3System.out.println("showJSONArray:");sf.showJSONArray();// 4System.out.println("showJSONObjectAddJSONArray:");sf.showJSONObjectAddJSONArray();// 5System.out.println("getJSONObjectToBean:");sf.getJSONObjectToBean();// 6System.out.println("getBeanToJSONObject:");sf.getJSONObjectToBean();// 7System.out.println("getListToJSONArray:");sf.getListToJSONArray();// 8System.out.println("getJSONObjectToList:");sf.getJSONObjectToList();// 9System.out.println("getMapToJson:");sf.getMapToJson();// 10System.out.println("getJsonToMap:");sf.getJsonToMap();// 11System.out.println("getJSONArrayToList:");sf.getJSONArrayToList();// 12System.out.println("getJSONArrayToArray:");sf.getJSONArrayToArray();// 13System.out.println("getArrayToJSONArray:");sf.getArrayToJSONArray();// 14System.out.println("getXmlToJSON:");sf.getXmlToJSON();// 15System.out.println("getJSONToXml:");sf.getJSONToXml();}}

Json字符串转对象和转List集合操作(alibabab版本)

https://blog.csdn.net/liangmaoxuan/article/details/80640259

总结不好多多担待,文章只单纯个人总结,如不好勿喷,技术有限,有错漏麻烦指正提出。本人QQ:373965070

Json字符串转对象和转List集合操作(json-lib版本)相关推荐

  1. Json字符串转对象和转List集合操作(alibabab版本)

    Json字符串转对象和转List集合操作(alibabab版本) Json是当前开发用得最多基于JavaScript语言的轻量级的数据交换格式,总结一下常用转换格式的方法,以便日后使用方便 以下为 a ...

  2. json字符串转对象的几种方式

    1. java自带的原生sf.json json字符串转对象 import net.sf.json.JSONObjectString response="{\"status\&qu ...

  3. Json字符串和对象相互转换

    文章目录 1.JsonUtil 工具类 把对象转换为json字符串 把json字符串转换为对象 把json字符串转换为List集合 2.Gson 把对象转换为json字符串 把json字符串转换为对象 ...

  4. c#谷歌 json转对象_利用Google Gson实现JSON字符串和对象之间相互转换

    最近一个项目需要用到JSON,需要在JSON字符串和对象之间相互转换,在网上找了些资料,发现google的Gson还是比较不错的. 废话不说,下面是简单的例子: 先上源码:下载(包含jar包) Per ...

  5. .NET后台字符中转JSON,和JSON字符串转对象

    长的字符串转为对象后,更容易循环遍历操作 string allSQL = "";//以下是多个JSON字符串的集合,先拆分 //strDrugJSON = [{"drug ...

  6. 开发:随笔记录之 Json字符串和对象的相互转换

    引入的包 :json-lib-2.1.jar import net.sf.json.JSONArray; import net.sf.json.JSONObject; //随意创建一个实体 RuleD ...

  7. JSON字符串和对象之间的转换

    使用jackSON来实现JSON字符串和对象之间的转换 引入maven <!--jackson--><dependency><groupId>com.fasterx ...

  8. json字符串和对象的相互转换

    大家好呀,我是柚子,今天这篇文章介绍的是json字符串和对象的相互转换~ 文章目录 举例 一.json字符串转对象 1.单个对象 2.多个对象 二.对象转json字符串 1.第一种方式 2.第二种方式 ...

  9. 将Json字符串转为对象JSONObject

    将Json字符串转为对象JSONObject 有些JSON类型的字符串无法直接转对象,需要先做处理.例如dataWrap.collectData()收集的数据: jsonStr = {"da ...

最新文章

  1. python操作mysql数据库依赖包_python安装mysql的依赖包mysql-python操作
  2. sqlserver 批量插入数据(此方式同样适用mysql)
  3. 2017-2-19 C#基础 基本数据类型的转换,转义字符,常量
  4. hihoCoder #1449 : 后缀自动机三·重复旋律6
  5. python微信聊天机器人源码_8.【代码】微信聊天机器人(API的应用) - Python网络爬虫实战...
  6. 信息学奥赛C++语言: 求正整数2和n之间的完全数
  7. [笔记].浅析在Nios II中的两种寄存器映射方法的异同
  8. asp.net 提取html div,asp.net – 将div固定在html中的某一点
  9. ARM处理器的9种模式详解
  10. 人工智能AI系列 - 视频图像搜索
  11. AI头发笔刷_1500款设计字体,海量PS笔刷,icon模板免费送!还为设计发愁吗?
  12. 手把手教你使用R语言绘制交互效应的森林图
  13. Amaze UI的分页设计
  14. 「第五章」点击劫持(ClickJacking)
  15. 华为手机怎么使用读卡器_华为手机怎么绑定门禁卡
  16. 将mybatis打印的Preparing与Parameters转化为可执行sql
  17. Struts常见错误及原因分析
  18. [BZOJ4084][Sdoi2015]bigyration hash
  19. linux如何kill僵尸进程,linux 如何杀死僵尸进程——原理及操作
  20. 摸鱼软件1:自动抓屏截图PPT软件

热门文章

  1. vue校验密码的三种写法
  2. 陕西计算机考研难度排行榜,陕西地区计算机考研院校分析「建议收藏」「最全」...
  3. 【HBase】HBase数据库基本操作(Shell)
  4. **Hadoop Ubuntu系统搭建攻略全详细!!!附带Hadoop搭建成功后测试案例**
  5. 网络学习 2g 3g 4g 技术对比 带宽理解 三大运营商手机网络模式 (制式)
  6. 【Sprite Atlas】Unity新图集系统SpriteAtlas超详细使用教程
  7. Latex 的交叉引用
  8. PS做以图片为文字背景
  9. 整理38款国外的一些免费虚拟主机,云计算,看看你使用过哪几个
  10. (6)自旋模型基态算法