java构造json数组_Java构造和解析Json数据的两种方法详解一

一、介绍

JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包,主要就是用来解析Json数据,在其官网http://www.json.org/上有详细讲解,有兴趣的可以去研究。

二、下载jar依赖包:可以去这里下载

tMEeYU2n9zwAAAAASUVORK5CYII=

三、基本方法介绍

1. List集合转换成json方法

List list = newArrayList();

list.add( "first");

list.add( "second");

JSONArray jsonArray2 = JSONArray.fromObject( list );

2. Map集合转换成json方法

48304ba5e6f9fe08f3fa1abda7d326ab.png

Map map = newHashMap();

map.put("name", "json");

map.put("bool", Boolean.TRUE);

map.put("int", new Integer(1));

map.put("arr", new String[] { "a", "b"});

map.put("func", "function(i){ return this.arr[i]; }");

JSONObject json = JSONObject.fromObject(map);

48304ba5e6f9fe08f3fa1abda7d326ab.png

3. Bean转换成json代码

JSONObject jsonObject = JSONObject.fromObject(new JsonBean());

4. 数组转换成json代码

boolean[] boolArray = new boolean[] { true, false, true};

JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

5. 一般数据转换成json代码

JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );

6. beans转换成json代码

48304ba5e6f9fe08f3fa1abda7d326ab.png

List list = newArrayList();

JsonBean2 jb1 = newJsonBean2();

jb1.setCol(1);

jb1.setRow(1);

jb1.setValue("xx");

JsonBean2 jb2 = newJsonBean2();

jb2.setCol(2);

jb2.setRow(2);

jb2.setValue("");

list.add(jb1);

list.add(jb2);

JSONArray ja = JSONArray.fromObject(list);

48304ba5e6f9fe08f3fa1abda7d326ab.png

四、演示示例

这里以基本的几个常用方法进行测试

48304ba5e6f9fe08f3fa1abda7d326ab.png

packagecom.json;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importnet.sf.json.JSONArray;

importnet.sf.json.JSONObject;

/*** 使用json-lib构造和解析Json数据

*

* @authorAlexia

* @date 2013/5/23

*

*/

public classJsonTest {

/*** 构造Json数据

*

* @return

*/

public staticString BuildJson() {

//JSON格式数据解析对象

JSONObject jo = newJSONObject();

//下面构造两个map、一个list和一个Employee对象

Map map1 = new HashMap();

map1.put("name", "Alexia");

map1.put("sex", "female");

map1.put("age", "23");

Map map2 = new HashMap();

map2.put("name", "Edward");

map2.put("sex", "male");

map2.put("age", "24");

List list = new ArrayList();

list.add(map1);

list.add(map2);

Employee employee = newEmployee();

employee.setName("wjl");

employee.setSex("female");

employee.setAge(24);

//将Map转换为JSONArray数据

JSONArray ja1 =JSONArray.fromObject(map1);

//将List转换为JSONArray数据

JSONArray ja2 =JSONArray.fromObject(list);

//将Bean转换为JSONArray数据

JSONArray ja3 =JSONArray.fromObject(employee);

System.out.println("JSONArray对象数据格式:");

System.out.println(ja1.toString());

System.out.println(ja2.toString());

System.out.println(ja3.toString());

//构造Json数据,包括一个map和一个Employee对象

jo.put("map", ja1);

jo.put("employee", ja2);

System.out.println("\n最终构造的JSON数据格式:");

System.out.println(jo.toString());

returnjo.toString();

}

/*** 解析Json数据

*

* @paramjsonString Json数据字符串

*/

public static voidParseJson(String jsonString) {

//以employee为例解析,map类似

JSONObject jb =JSONObject.fromObject(jsonString);

JSONArray ja = jb.getJSONArray("employee");

List empList = new ArrayList();

//循环添加Employee对象(可能有多个)

for (int i = 0; i < ja.size(); i++) {

Employee employee = newEmployee();

employee.setName(ja.getJSONObject(i).getString("name"));

employee.setSex(ja.getJSONObject(i).getString("sex"));

employee.setAge(ja.getJSONObject(i).getInt("age"));

empList.add(employee);

}

System.out.println("\n将Json数据转换为Employee对象:");

for (int i = 0; i < empList.size(); i++) {

Employee emp =empList.get(i);

System.out.println("name: " + emp.getName() + " sex: "

+ emp.getSex() + " age: " +emp.getAge());

}

}

/*** @paramargs

*/

public static voidmain(String[] args) {

//TODO Auto-generated method stub

ParseJson(BuildJson());

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

运行结果如下

EaYZcp4ZpkEAAAAASUVORK5CYII=

转:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值