首页 > 编程语言 > 详细

在JAVA中使用JSONObject生成json

时间:2014-09-22 11:45:13      阅读:288      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/qiaoxu/p/3985616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!