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. QTP动态加载对象库

    Public Function AddObjectRepository(path) On Error Resume Next Dim pos, repath If instr(path,". ...

  2. 学习练习 java 二分查找法

    package com.hanqi; import java.util.*; public class Test5 { public static void main(String[] args) { ...

  3. BFPRT(线性查找算法)

    BFPRT算法解决的问题十分经典,即从某n个元素的序列中选出第k大(第k小)的元素,通过巧妙的分 析,BFPRT可以保证在最坏情况下仍为线性时间复杂度.该算法的思想与快速排序思想相似,当然,为使得算法 ...

  4. 线程中CreateEvent和SetEvent及WaitForSingleObject的用法

    首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面. CreateEvent 函功能描述:创建或打开一个命名的或无名的事件对象. EVENT有两种状态 ...

  5. JDBC链接MySQL和Oracle

    import java.sql.*;         JDBC中所要用的包几乎都在import?java.sql.*;中: 在项目中导入Oracel或者是MySQL包和装载驱动:     项目的Cla ...

  6. JS与JQ倒计时的写法

    页面需要制作一个倒计时的功能:然后度娘了一遍,找到两种写法,原生JS与JQ 的,经过测试原生JS在IE可能会有不刷新的现象所以结合了一个大神的JQ写法修改好了一个. 原生JS写法: HTML: < ...

  7. JMeter笔记3:聚合报告之90%Line参数说明

    看看JMeter 官网是怎么说的? 90% Line - 90% of the samples took no more than this time. The remaining samples a ...

  8. C#中string类型前加@标志的作用

    转自:http://stackoverflow.com/questions/4879152/c-sharp-before-a-string   (stackoverflow) string字符串前加@ ...

  9. MyBatis学习系列一之环境搭建

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 学习一个新的知识,首先做一个简单的例子使用一下,然后再逐步深入.MyBat ...

  10. [原]POJ1141 Brackets Sequence (dp动态规划,递归)

    本文出自:http://blog.csdn.net/svitter 原题:http://poj.org/problem?id=1141 题意:输出添加括号最少,并且使其匹配的串. 题解: dp [ i ...