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. 出现win8.1蓝屏重启故障该如何解决?

    出现win8.1蓝屏重启故障该如何解决?电脑出现win8.1蓝屏重启故障,系统提示“你的电脑遇到问题,需要重新启动,我们只收集某些错误信息,然后为你重新启动.”怎么解决?本教程为你提供了win8.1蓝 ...

  2. javaScript 封装

    在基于web 的b/s 架构的项目中, 丰富的界面都离不开 javascript, javascript 在 html 中变得越来越强大,但是我们在写 javascript 的时候都比较随意,随着页面 ...

  3. js控制台调试

    在web编程的过程中js代码出现错误,可以通过console.log()将相关信息输入到控制台进行调试. 清空控制台右击选 Clear console 菜单,或者输入 clear() 都行 控制台相关 ...

  4. 《Unix/Linux日志分析与流量监控》书稿完成

    <Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...

  5. 常用颜色的RGB值

    RGB颜色表 白色:rgb(255,255,255) 黑色:rgb(0,0,0) 红色:rgb(255,0,0) 绿色:rgb(0,255,0) 蓝色:rgb(0,0,255) 青色:rgb(0,25 ...

  6. leetcode007. Reverse Integer

    /* a good way to predict overflow * each time *10 must predict int overflow * not only the last time ...

  7. 关于<html>标签里的class= no-js

    最近研究bootstrap的时候,发现了一个案例网站有一段 <html class="no-js"> 然后问了下度娘,找到了一篇大神的文章 里面对这个有很详细的解释: ...

  8. Javascript之严格模式详解

    一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...

  9. wifi热点共享 3G网络

    本文描述一些iptables的基础知识和使用方法.最后记录一个wifi共享3G上网,以及禁止wifi内某个IP通过3G联网的例子. 一. 在Linux系统中,防火墙,网址转换(NAT),数据包记录以及 ...

  10. Dynamic Prompt Table for Record Field on PeopleSoft Page

    Sometimes a situation in project work arises to have a dynamic prompt table for record fields on Peo ...