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. 图的最短路算法 Floyd

    多源最短路径算法 时间复杂度O(N3) 简单修改可求有向图的传递闭包 #include<iostream> using namespace std; const int maxn=1024 ...

  2. 洛谷P1268 树的重量

    P1268 树的重量 85通过 141提交 题目提供者该用户不存在 标签树形结构 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 有这种情况吗!!!! 题意似乎有问题 题目描述 树可以用来表 ...

  3. phonegap ios默认启动页

    phonegap创建的项目默认的启动界面是phonegap的图标,想去掉这个图标,有两个方法,第一就是将resourece下面的splash文件下面的图片改成自己想要的启动页面,名字要相同,替换掉它默 ...

  4. span标签之间的空隙

    出现的问题: 在html中,当有两个以及两个以上的span标签并列的时候,如果任意两个span之间换行书写的话,那么他们在页面上展现的时候往往会有空隙 解决的办法有两个: 1.将两个span标签写在同 ...

  5. Multithreading annd Grand Central Dispatch on ios for Beginners Tutorial-多线程和GCD的入门教程

    原文链接:Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial Have you ever written a ...

  6. Java中函数的重载

    函数的重载 1.同一个类 2.同名函数 3.参数个数不同或者参数类型不同 4.java是严谨性语言,如果函数出现的调用的不确定性,会编译失败. public static int add(int a, ...

  7. Longest Common Prefix [LeetCode 14]

    1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...

  8. 模拟cpu调度

    先来先服务实现简单但是平均周转时间过长 短作业优先算法缩短了平均周转时间 #!/usr/bin/python #-*- coding: utf-8 -*- # # table # 0:进程号 1:到达 ...

  9. Splash Screen开场屏在Android中的实现

    很多网友可能发现近期Tencent推出的手机QQ Android版包含了一个开场屏Splash Screen载入效果,通常游戏或大型软件打开时可能需要一个释放解析资源的过程,需要一个前台的动画播放和后 ...

  10. [转载]《C++0x漫谈》系列之:多线程内存模型

    <C++0x漫谈>系列之:多线程内存模型 By 刘未鹏(pongba) 刘言|C++的罗浮宫(http://blog.csdn.net/pongba) <C++0x漫谈>系列导 ...