首先声明一下,jsonlib转换与GSON相比太差劲了,操作不是一般的繁琐。GSON可以直接转换成各种集合与对象类型。强烈推荐使用GSON。而且GSON一个方法就可以解决,jsonlib转来转去太繁琐了。

手动创建JSONObject与JSONArray有用,用于读取文件对字符串进行处理

-----------------------------jsonlib操作复杂,转换Map与list<map>更是复杂----------------

Jar包

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmUAAACoCAIAAAAuBA4zAAAgAElEQVR4nO2dz28TRxvH+QN6e289V/wDvVSReufGDQW1tJfIcAg9oLRSgCCIKNtUJbJECocIaiq1IIqIhYhbB0UIBKX0RARBDnb8A5vs2qzBkuNWTVPlPax3d348M7tx7NhOvh9FkT07M/vsej3ffZ4Z77NnAwAAAABB7Om2AQAAAEAfAL0EAAAAgoFeAgAAAMFALwEAAIBgoJcAAABAMNBLAAAAIBjoJQAAABAM9BIAAAAIRtTLVKF8++GzH2b/CPy7/fBZqlDuitEAAADANiPq5cz9p4svs41G42+J1dVV73Wj0Vh8mZ25/7QrRgMAAADbjKiXF2/eq1ar6+vr/zGsr6+vrq7Wlh6ura15JdVq9eLNe92wGQCwO7GSxtHYQretALsVUS+j1+Zs2/6XYW1trV6vV+9+Z09+1Gg0vHLbtqPX5rpidAdIRwf2RBJd2e1ANL2xsZGIdGP/CmMA6FkWYkeNpEVv4TYsxCCtoL2IejlxdbZcLv/DUKvVKr9NvPl677t3794szDUaDae8XC5PXJ3titEdoP/0Mh0doMUtHR3Y40BuT0SaW6Xd7RK9XIgdbcKOrl6pOBZbSYMvUtZknB+vjjRge3VoM/xdKDRBMsnvR9md9ig6KSmitFlJQ9ihlTT0pvMVSNxWnlw2X0AvNwt5TcqFIS457tw7n6Fw1auu1N5G1Mvx6RnTNP9yqdfr1vxU+dT7pmlad8YrZ/fW63Vnk2ma49MzXTG6A2ynXpL72oxeuopIiRvTeSIiiWI6OqDeuCtgfJCFmDt4W0njqDfW+iO6O1Yz32tFTXebkbQ2Nqyk0SyXqnh1SDOYfagG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QC8BAACAYKCXAAAAQDDQSwAAACAY6CUAAAAQDPQSAAAACAZ6CQAAAAQDvQQAAACCgV4CAAAAwUAvAQAAgGCglwAAAEAw0EsAAAAgGOglAAAAEAz0EgAAAAgGegkAAAAEA70EAAAAgoFeAgAAAMFALwEAAIBgoJcAAABAMNBLAAAAIBjoJQAAABAM9BIAAAAIBnoJAAAABAO9BAAAAIKBXgIAAADBQC8BAACAYKCXAAAAQDDQSwAAACAY6CUAAAAQDPQSAAAACAZ6CQAAAAQDvQQAAACCgV4CAAAAwUAvAQAAgGCglwAAAEAw0EsAAAAgmP8Dwtqi8TraEIQAAAAASUVORK5CYII=" alt="" />

User.java

 package TestJson_JSONlib;

 public class User {

     private String id;
private String name;
private Integer age;
private String address;
public String getId() {
return id;
}
public int getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
}
public User(String id, String name, Integer age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
} }

1.Java对象转json(操作复杂)

对象类型用JSONObject,集合类型用JSONArray。

package TestJson_JSONlib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.xml.bind.UnmarshallerHandler; import org.junit.Test; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class Bean2JSON { // 测试单个对象转JSON,转单个对象用JSONObject
@Test
public void test1(){
User user = new User("1", "QIAOZHI",25,"山西太原");
JSONObject jsonObject = JSONObject.fromObject(user);
String json = jsonObject.toString();
System.out.println(json);
//{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}
} // 测试list转JSON,转集合用JSONArray,会产生[]
@Test
public void test2(){
List<User> list = new ArrayList<User>();
list.add(new User("1", "QIAOZHI",25,"山西太原"));
list.add(new User("2", "QIAOZHI",25,"山西太原"));
list.add(new User("3", "QIAOZHI",25,"山西太原"));
JSONArray jsonArray = JSONArray.fromObject(list);
String json = jsonArray.toString();
System.out.println(json);
// [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}]
} // 测试map对象转JSON,转单个对象用JSONObject
@Test
public void test3(){
Map<String,User> map = new HashMap<String,User>();
map.put("1", new User("1", "QIAOZHI",25,"山西太原"));
map.put("2", new User("2", "QIAOZHI",25,"山西太原"));
map.put("3", new User("3", "QIAOZHI",25,"山西太原"));
JSONObject jsonObject= JSONObject.fromObject(map);
String json = jsonObject.toString();
System.out.println(json);
// {"3":{"address":"山西太原","age":25,"id":"3","name":"QIAOZHI"},"2":{"address":"山西太原","age":25,"id":"2","name":"QIAOZHI"},"1":{"address":"山西太原","age":25,"id":"1","name":"QIAOZHI"}}
} // 测试list<map>对象转JSON,转集合用JSONArray,会产生[]
@Test
public void test4(){
List<Map<String,User>> list = new ArrayList<>();
Map<String,User> map1 = new HashMap<String,User>();
Map<String,User> map2 = new HashMap<String,User>();
map1.put("map11", new User("1", "QIAOZHI",25,"山西太原"));
map1.put("map12", new User("2", "QIAOZHI",25,"山西太原"));
map2.put("21", new User("11", "QIAOZHI",25,"山西太原"));
map2.put("22", new User("21", "QIAOZHI",25,"山西太原"));
list.add(map1);
list.add(map2);
JSONArray jsonArray = JSONArray.fromObject(list);
String json = jsonArray.toString();
System.out.println(json);
// [{"map12":{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},"map11":{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}},{"21":{"id":"11","name":"QIAOZHI","age":25,"address":"山西太原"},"22":{"id":"21","name":"QIAOZHI","age":25,"address":"山西太原"}}]
} }

2.JSON字符串转Java对象与list(只测了转bean与list)

转出来的中间还有空格,使用时候还得去空格。太繁琐了。

在将 Json 形式的字符串转换为 JavaBean 的时候需要注意 JavaBean 中必须有无参构造函数,否则会报找不到初始化方法的错误。

package TestJson_JSONlib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Test; import com.google.gson.Gson; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JSON2Bean {
// 测试json转单个对象
@Test
public void test1(){
String json = "{\"id\":\"1\",\"name\":\"QIAOZHI\",\"age\":25,\"address\":\"山西太原\"}";
JSONObject jsonObject = JSONObject.fromObject(json);
System.out.println(jsonObject); //产生一个json格式的json对象
// {"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}
User user = (User) JSONObject.toBean(jsonObject, User.class);
System.out.println(user);
// User [id=1, name=QIAOZHI, age=null, address=山西太原]
} // 测试JSON转list(太反复杂了)
@SuppressWarnings("unchecked")
@Test
public void test2(){
String json = "[{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'},{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},{'id':'3','name':'QIAOZHI','age':25,'address':'山西太原'}]";
List<User> list = new ArrayList<User>();
JSONArray jsonArray = JSONArray.fromObject(json);
System.out.println(jsonArray); //产生一个JSON格式的数组
// [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}]
list = (List<User>) JSONArray.toList(jsonArray,User.class);
System.out.println(list);
// [User [id=1, name=QIAOZHI, age=null, address=山西太原], User [id=2, name=QIAOZHI, age=null, address=山西太原], User [id=3, name=QIAOZHI, age=null, address=山西太原]]
System.out.println(list.get(1));
// User [id=2, name=QIAOZHI, age=null, address=山西太原]
}
}

3.手动创建JSONObject与JSONArray(用于封装JSON转java对象与List集合)

package TestJson_JSONlib;

import org.junit.Test;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class CreateJSONObject { @Test
public void test1(){
// 手动创建一个JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", "11");
jsonObject.put("name", "qiaozhi");
jsonObject.put("age", 30);
jsonObject.put("address", "山西太原");
System.out.println(jsonObject);
// {"id":"11","name":"qiaozhi","age":30,"address":"山西太原"} JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", "12");
jsonObject1.put("name", "qiaozhi");
jsonObject1.put("age", 30);
jsonObject1.put("address", "山西太原");
System.out.println(jsonObject1);
// {"id":"12","name":"qiaozhi","age":30,"address":"山西太原"} // 产生一个JSONArray
JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonObject);
jsonArray.add(jsonObject1);
System.out.println(jsonArray);
// [{"id":"11","name":"qiaozhi","age":30,"address":"山西太原"},{"id":"12","name":"qiaozhi","age":30,"address":"山西太原"}] // 用上述方法转成Java对象与List集合,用于处理数据
}
}

可用于手动封装JSON对象。

补充:JSON字符串转map,与map转json字符串的测试:(其实字符转转map是先转为JSONObject再转为map)

        String accessToken = FetchData.getAccessToken();//{"expires_in":7200,"errmsg":"ok","access_token":"94a50870f92230eb8ad8211b4023675c","errcode":0}

        //*******************************json字符串转map
//转为JSONObject
JSONObject jsonObject = JSONObject.fromObject(accessToken);
//转为Map
Map map = (Map) JSONObject.toBean(jsonObject, Map.class);
System.out.println(map); //******************************Map转JSON字符串
JSONObject jsonStr = JSONObject.fromObject(map);
System.out.println(jsonStr.toString());

结果:

{expires_in=7200, errmsg=ok, access_token=94a50870f92230eb8ad8211b4023675c, errcode=0}
{"expires_in":7200,"errmsg":"ok","access_token":"94a50870f92230eb8ad8211b4023675c","errcode":0}

补充:JSONArray其实是List的子类,转换的时候字符串以[]包裹的可以转为JSONArray,而且要转为JSONArray必须以[    ]开始结束

        String jsonStrs = "[{\"customerId\":19,\"customerName\":\"\tXXX有限公司\"},{\"customerId\":5568,\"customerName\":\"\tXXX展有限公司\"}]";
// 字符串转为JSONArray,必须以[开始,以]结束
JSONArray jsonArray = JSONArray.fromObject(jsonStrs);
System.out.println(jsonArray.size());
System.out.println(jsonArray.toString());
System.out.println("=================================================");
for (int i = 0, length_1 = jsonArray.size(); i < length_1; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.toString());
} // jsonArray转list
System.out.println("=================================================");
List list = JSONArray.toList(jsonArray);
System.out.println(list);

结果:

2
[{"customerId":19,"customerName":"\tXXX有限公司"},{"customerId":5568,"customerName":"\tXXX展有限公司"}]
=================================================
{"customerId":19,"customerName":"\tXXX有限公司"}
{"customerId":5568,"customerName":"\tXXX展有限公司"}
=================================================
[net.sf.ezmorph.bean.MorphDynaBean@20c8e08f[
{customerName= XXX有限公司, customerId=19}
], net.sf.ezmorph.bean.MorphDynaBean@640edd19[
{customerName= XXX展有限公司, customerId=5568}
]]

查看JSONArray源码:

补充2:其实我们的JSONObject就是Map,我们将字符串转为JSONObject之后数据存入其内置的map中:

        String returnedStr = HttpUtils.doGetWithParams(url, params);//{"errmsg":"ok","department":[{"id":1,"createDeptGroup":true,"name":"开发测试","autoAddUser":true}],"errcode":0}
// *******************************json字符串转map
JSONObject jsonObject = JSONObject.fromObject(returnedStr);
if (jsonObject != null && "ok".equals(jsonObject.get("errmsg"))) {
return (String) jsonObject.get("department");
} return null;

结果:

[{"id":1,"createDeptGroup":true,"name":"开发测试","autoAddUser":true}]

查看源码:

   public Object get( Object key ) {
if( key instanceof String ){
return get( (String) key );
}
return null;
} /**
* Get the value object associated with a key.
*
* @param key A key string.
* @return The object associated with the key.
* @throws JSONException if this.isNull() returns true.
*/
public Object get( String key ) {
verifyIsNull();
return this.properties.get( key );
}

  如果是字符串的话调用字符串调用方法从properties中获取值,properties是什么?应该是个map。。。继续查看源码如下:

   /**
* The Map where the JSONObject's properties are kept.
*/
private Map properties;

补充2:如果从JSONObject中获取一个不存在的key的时候会报错,而不是返回null,查看源码发现:

   public String getString( String key ) {
verifyIsNull();
Object o = get( key );
if( o != null ){
return o.toString();
}
throw new JSONException( "JSONObject[" + JSONUtils.quote( key ) + "] not found." );
}

解决办法:先判断一下

                if(jo.containsKey("tel")){
userMap.put("officePhone", jo.getString("tel"));
}

而对于HashMap来说,key不存在是返回null的,源码如下:

    public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue();
}

总结:

  1.字符串转JSONArray的时候字符串必须是JSON数组,也就是[{},{},.....];字符串转JSONObject必须是对象,也就是{"key":"value"...},当多个{}{}的时候只会转一个,如下。字符串转JSONArray或者JSONObject调用对应的fromObject(Obj)方法:

        String jsonStrs = "[{\"customerId\":19,\"customerName\":\"\tXXX有限公司\"},{\"customerId\":5568,\"customerName\":\"\tXXX展有限公司\"}]";
String jsonStrs1 = "{\"customerId\":19,\"customerName\":\"\tXXX有限公司\"},{\"customerId\":5568,\"customerName\":\"\tXXX展有限公司\"}";
JSONArray fromObject = JSONArray.fromObject(jsonStrs);
JSONObject fromObject1 = JSONObject.fromObject(jsonStrs1);
System.out.println(fromObject.toString());
System.out.println(fromObject1.toString());

结果:

[{"customerId":19,"customerName":"\tXXX有限公司"},{"customerId":5568,"customerName":"\tXXX展有限公司"}]
{"customerId":19,"customerName":"\tXXX有限公司"}

  2.JSONObject是一个中间对象。如果操作集合调用对应的JSONArray即可。

    字符串转对象可以先调用JSONObject.fromObject(str)转换为JSONObject,然后调用JSONObject.toBean(jsonObject,class)转为对应类型的对象

    对象转JSON字符串可以JSONObject.fromObject(str)转换为JSONObject,然后调用其toString()方法即可。

        Map map = new HashMap<>();
map.put("x", "x");
map.put("y", "y");
// Map转JSON字符串(先为JSONObject)
JSONObject fromObject = JSONObject.fromObject(map);// 转为JSONObject
String jsonStr = fromObject.toString();
System.out.println(jsonStr);
System.out.println("==========================");
// JSON转Map字符串(先转为JSONObject)
JSONObject fromObject2 = JSONObject.fromObject(jsonStr);
Map bean = (Map) JSONObject.toBean(fromObject2, Map.class);
System.out.println(bean);

结果:

{"y":"y","x":"x"}
==========================
{y=y, x=x}

补充:上面的JSONObject是JSONLib下的类。如果是fastjson包下的JSONObject(alibaba的)使用更简单,字符串转对象与对象转字符串如下:

  toJSONString(bean)转为JSON串,parseObject(jsonStr,class)转为Bean。

        Map map = new HashMap<>();
map.put("x", "x");
map.put("y", "y");
// bean转JSON字符串
String jsonString = com.alibaba.fastjson.JSONObject.toJSONString(map);
System.out.println(jsonString); // JSON字符串转bean
Map parseObject = com.alibaba.fastjson.JSONObject.parseObject(jsonString, Map.class);
System.out.println(parseObject);

也可以指定指定属性不进行序列化,如下:

        Map<String, String> map = new HashMap<>();
map.put("x", "x");
map.put("y", "y"); // 采用过滤器过滤掉指定属性
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("x");
String jsonString2 = com.alibaba.fastjson.JSONObject.toJSONString(map, filter);
System.out.println(jsonString2);

结果:

{"y":"y"}

Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)的更多相关文章

  1. python 面向对象:封装---对象的属性可以是另一个类创建的对象

    # 对象封装:对象的属性可以是另一个类创建的对象 # 案例需求: # 1.士兵许三多有一把AK47 # 2.士兵用枪射击标靶 # 3.枪能装填和发射子弹 class Gun: # 分析: # 枪的属性 ...

  2. java 字符串转json,json转实体对象、json字符串转换成List、List转String、以及List排序等等...

    @RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...

  3. js数组,字符串,json互相转换函数有哪些

    js数组,字符串,json互相转换函数有哪些 一.总结 一句话总结: JSON.stringify(arr) JSON.parse(jsonString) str.split('') array.jo ...

  4. Java 关于创建String对象过程的内存分配

    一.String s = "abc"  和 String s = new String("abc") 的区别 1.String s = "abc&qu ...

  5. jenkins:实现Jenkinsfile与Json的转换

    实现Jenkinsfile与Json的转换 目录 实现Jenkinsfile与Json的转换 方法1:使用现有的jenkins插件 参考 方法2:解析原生的jenkinsfile文件 参考 最近在做个 ...

  6. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入

    本节主要内容:       1.实例化Spring容器示例    2.利用Spring容器创建JavaBean对象    3.如何控制Bean实例化    4.利用Spring实现bean属性sett ...

  8. 《Java程序员面试笔试宝典》之字符串创建与存储的机制是什么

    在Java语言中,字符串起着非常重要的作用,字符串的声明与初始化主要有如下两种情况: (1)       对于String s1=new String("abc")语句与Strin ...

  9. Json.Net系列教程 4.Linq To JSON

    原文 Json.Net系列教程 4.Linq To JSON 更改历史 2013-05-31 添加一个FAQ   一.Linq to JSON是用来干什么的? Linq to JSON是用来操作JSO ...

随机推荐

  1. 有源汇有上下界最小流 DInic + 各种优化 模板

    例题:loj117 : https://loj.ac/problem/117 //其实就是判断可行流后倒着求一遍最大流 #include <iostream> #include <c ...

  2. 【AtCoder010】B - Boxes(差分)

    AtCoder Grand Contest 010 B题 题目链接 题意 n个盒子,第i个盒子有ai个石头. 重复这个步骤:选一个盒子i,每次从第i+j个盒子中移走j个石头,j从1到n,第n+k个盒子 ...

  3. 洛谷 P1158 导弹拦截(不是那个DP) 解题报告

    P1158 导弹拦截 题目描述 经过1111年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截.当工作半径为0时,则能够拦截与它位置恰好相同的导弹. ...

  4. 洛谷 P1407 [国家集训队]稳定婚姻 解题报告

    P1407 [国家集训队]稳定婚姻 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的 ...

  5. vlc sdl 播放视频可随窗口改变大小

    #include <stdio.h> #include <stdint.h> #include <math.h> #include <stdlib.h> ...

  6. X11,GTK,QT,GNOME的区别与联系(UI工具总结)

    1,X11是X Window System Protocol, Version 11(RFC1013),是X server和X client之间的通信协议.X server是xfree86/xorg驱 ...

  7. Office2016自定义安装

    Office2016已经不提供自定义安装功能,而采用C2R安装方式.使用镜像安装时,默认全部安装.想要自定义安装就需要用到微软提供的Office2016部署工具. 步骤 下载并运行微软提供的Offic ...

  8. xrandr: 命令行修改分辨率工具

    常用功能举例 查看支持的分辨率 xrandr -q 将分辨率设为1024×768 xrandr -s 1024×768 恢复到默认分辨率 xrandr -s 0

  9. 【LOJ#6282】数列分块6

    题目大意:给定一个由 N 个数组成的序列,维护两种操作:单点询问,单点插入.N < 100000 题解:在块内维护一个链表,支持动态插入数字,同时对于非随即数据来说,若块的大小过大,需要重构. ...

  10. bug7 java.net.BindException: 端口被占用。

    java.net.BindException:   端口被占用. java.net.BindException:   端口被占用.