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的更多相关文章

  1. JAVA中的四种JSON解析方式详解

    JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...

  2. 在java中调用mockjs生成模拟数据

    一.手写版 在前端有个模拟数据的神器 Mock.js 能生成随机数据,拦截 Ajax 请求,然后我觉得他的这个生成随机数据不错.然后我就到度娘一顿操作,没找到类似的java实现,于是就有了下面的代码: ...

  3. JAVA中使用freemark生成自定义文件(json、excel、yaml、txt)

    原文:http://blog.csdn.net/jinzhencs/article/details/51461776 场景:在我们工作中,有时需要生成一些文件,可能它不是一种标准的格式,比如JSON. ...

  4. java用JSONObject生成json

    Json在前后台传输中,是使用最多的一种数据类型.json生成的方法有很多,自己只是很皮毛的知道点,用的时候,难免会蒙.现在整理下 第一种: import net.sf.json.JSONArray; ...

  5. java中使用net.sf.json对json进行解析

    net.sf.json依赖的包很多. 有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.ja ...

  6. JAVA中使用JSONObject对象必备

    JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.本文将快速讲解 JSON 格式,并通过代码示例演示如 ...

  7. Java 中判断 JSONObject 对应的 VALUE 为空

    目前发现有两种包.两种不一样的json包. 第一种情况是: json包是json-lib包是net.sf.json 怎样判断JSONObject返回的是字符串null还是null值. 研究源码发现.J ...

  8. Java中使用OpenSSL生成的RSA公私钥进行数据加解密

    当前使用的是Linux系统,已经按装使用OpenSSL软件包, 一.使用OpenSSL来生成私钥和公钥 1.执行命令openssl version -a 验证机器上已经安装openssl 1 open ...

  9. Java中使用OpenSSL生成的RSA公私钥

    RSA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的 ...

随机推荐

  1. 慕课网-安卓工程师初养成-2-10 Java中的强制类型转换

    来源:http://www.imooc.com/code/1241 相信小伙伴们也发现了,尽管自动类型转换是很方便的,但并不能满足所有的编程需要. 例如,当程序中需要将 double 型变量的值赋给一 ...

  2. 【练习】sqlnet.ora

    在SQLNET.ora文件中设置以下参数可以实现IP访问限制: $ pwd/u01/app/oracle/product/10.2.0/db_1/network/admin$ vi sqlnet.or ...

  3. Modifiers

    Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the ...

  4. AD转换精度的计算

    声明原文来源于:http://wenku.baidu.com/view/1e6d8f3083c4bb4cf7ecd1c2.html 讨论AD转换分辨率的算法(zt) (1)在总长度为5米的范围里,平均 ...

  5. Android基础总结(4)——广播接收器

    在Android中的每个应用程序可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能来自于系统的,也可能来自于其他应用程序的.Android提供了一整套完整的API, ...

  6. HTML5-新API选择器

    新的选择器document.querySelector("selector");selector:根据CSS选择器返回第一个匹配到的元素,如果没有匹配到,则返回null;支持: C ...

  7. Windows phone 8 学习笔记(4) 应用的启动(转)

    Windows phone 8 的应用除了可以直接从开始菜单以及应用列表中打开外,还可以通过其他的方式打开.照片中心.音乐+视频中心提供扩展支持应用从此启动.另外,我们还可以通过文件关联.URI关联的 ...

  8. leetcode 121

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  9. leetcode 13

    罗马数字是阿拉伯数字传入之前使用的一种数码.罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500). 记数的方法: 相同的数字连写,所 ...

  10. 011OK6410开发板介绍

    1.系统资源: (1)处理器:三星ARM11,S3C6410A,主频533MHz/667MHz (2)nor flash (3)nand flash:1G字节NAND Flash (4)RAM:128 ...