在JAVA中使用JSONObject生成json
JSON是一种轻量级的数据交换格式,在现在的web开发中,是非常常见的。在没有方便的工具之前,我们或许会使用拼字符串的形式来生成json数组,今天我们使用一个json-lib.jar包来为我们实现生成json的功能。
所必须的包有:
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar
1、bean转为Json
User u = new User(); u.setAge(22);
u.setUsername("hzucmj"); u.setEnabled(true);
JSONObject json = JSONObject.fromObject(u);
System.out.println(json.toString());
//结果为:
{"enabled":true,"username":"hzucmj","age":22}
2、List转为Json
User u1 = new User(); u1.setAge(22);
u1.setUsername("hzucmj");
u1.setEnabled(true);
User u2 = new User();
u2.setAge(20); u2.setUsername("ctf");
u2.setEnabled(true);
List<Object> list = new ArrayList<Object>();
list.add(u1);
list.add(u2);
JSONArray json = JSONArray.fromObject(list);
System.out.println(json.toString());
//结果为:
[{"enabled":false,"username":"ctf","age":20},{"enabled":false,"username":"","age":0}]
3、Map转为Json
HashMap<String, Comparable> map = new HashMap<String, Comparable>();
map.put("name", "hzucmj"); map.put("age", 22);
JSONObject json = JSONObject.fromObject(list);
System.out.println(json.toString());
//结果为:
{"name":"hzucmj","age":22}
基于json-lib.jar包 JSONObject-Java常用的四种用法
基于json-lib.jar包Json
实例程序 1.
JSONObject to DynaBean String json = "{name=\"json\",bool:true,int:1,double:2.2}";
JSONObject jsonObject = JSONObject.fromObject(json);
//抽象的写法:
DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject );
Object bean = JSONObject.toBean(jsonObject);
//Object bean1 = JSONSerializer.toJava(jsonObject);
assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean, "name"));
assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool"));
assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int"));
assertEquals(jsonObject.get("double"), PropertyUtils.getProperty(bean, "double"));
2.JSONObject to JavaBean String json = "{name:\"zhangsan\",age:25,hight:1.72,sex:true}";
JSONObject jsonObject = JSONObject.fromObject(json);
UserBean bean = (UserBean) JSONObject.toBean(jsonObject, UserBean.class);
System.out.println(jsonObject);
理论上,这样就可以了,但时,有异常Caused by: java.lang.NoSuchMethodException: com.json.Json$UserBean.<init>()
3.JSONArray to List String json = "[\"first\",\"second\"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
List output = (List) JSONSerializer.toJava(jsonArray);
4.JSONArray to array String json = "[\"first\",\"second\"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
Object[] output = (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig);
Object[] expected = new Object[] { "first", "second" };
ArrayAssertions.assertEquals(expected, output);
在JAVA中使用JSONObject生成json的更多相关文章
- JAVA中的四种JSON解析方式详解
JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...
- 在java中调用mockjs生成模拟数据
一.手写版 在前端有个模拟数据的神器 Mock.js 能生成随机数据,拦截 Ajax 请求,然后我觉得他的这个生成随机数据不错.然后我就到度娘一顿操作,没找到类似的java实现,于是就有了下面的代码: ...
- JAVA中使用freemark生成自定义文件(json、excel、yaml、txt)
原文:http://blog.csdn.net/jinzhencs/article/details/51461776 场景:在我们工作中,有时需要生成一些文件,可能它不是一种标准的格式,比如JSON. ...
- java用JSONObject生成json
Json在前后台传输中,是使用最多的一种数据类型.json生成的方法有很多,自己只是很皮毛的知道点,用的时候,难免会蒙.现在整理下 第一种: import net.sf.json.JSONArray; ...
- java中使用net.sf.json对json进行解析
net.sf.json依赖的包很多. 有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.ja ...
- JAVA中使用JSONObject对象必备
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.本文将快速讲解 JSON 格式,并通过代码示例演示如 ...
- Java 中判断 JSONObject 对应的 VALUE 为空
目前发现有两种包.两种不一样的json包. 第一种情况是: json包是json-lib包是net.sf.json 怎样判断JSONObject返回的是字符串null还是null值. 研究源码发现.J ...
- Java中使用OpenSSL生成的RSA公私钥进行数据加解密
当前使用的是Linux系统,已经按装使用OpenSSL软件包, 一.使用OpenSSL来生成私钥和公钥 1.执行命令openssl version -a 验证机器上已经安装openssl 1 open ...
- Java中使用OpenSSL生成的RSA公私钥
RSA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的 ...
随机推荐
- USACO Section 4.4 追查坏牛奶Pollutant Control
http://www.luogu.org/problem/show?pid=1344 题目描述 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件 ...
- 过河-状压DP
http://www.luogu.org/problem/show?pid=1052 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上 ...
- USACO Section 3.3 骑马修栅栏 Riding the Fences
题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个 ...
- JS基础学习1——什么是基础js类和原型?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Recover damage pictures to see the crime scene
Few people know that when you take photos there is also a thumbnail embeded inside the file, even so ...
- 开源自己的一个小android项目(美女撕衣服游戏)
这是自己的一个开源自己的一个小android项目(美女撕衣服游戏),也是前6个月开发的,有部分的资源来自网络上的,现在开源出来给大家吧,由于源码比较大,不上传了,我已经上传到源码天堂那个网站那里了,大 ...
- 网络HTTP请求状态详解
HTTP状态码大全 完整的 HTTP 1.1规范说明书来自于RFC 2616,你可以在http://www.talentdigger.cn/home/link.php?url=d3d3LnJmYy1l ...
- N层电梯只停一层情况下,求所有人爬楼层数最少
一.题目: 石家庄铁道大学基础教学楼一共有四部电梯,每层都有人上下,电梯在每一层都停.信1201-1班张一东每层都停有点儿不耐烦.如果在上下课高峰时刻电梯从一层上行,但只允许停留在某一层.每个人选择自 ...
- 遇到困难 jsp代码onclick="javascript:return(checklogin());"报错
<script language="javascript"> function checklogin() { if (document.getElementById(& ...
- Jquery 实现json复杂查询等操作(jsonDB)
一.jsonDB 下载地址:https://github.com/ThinkerCodeChina/jsonDB jsonDB是js的一个类库,实现使用SQL语句对json数据增删改查.jsonDB的 ...