JSON数据解析
1 JSON简介
JSON(JavaScript Object Notation),类似于XML,是一种轻量级的数据交换格式,比如在JavaEE中Struts2与Ajax在来回传递数据时,除了可以利用XML外,还可以利用JSON。在JavaScript中有两种JSON的语法,一种用于创建对象,另一种用于创建数组。
1.1 使用JSON语法创建对象
如图1.1所示,在创建object对象时,以“{”开始,以“}"结束。对象的每个属性名和属性值之间以英文冒号":"隔开,多个属性定义之间以英文逗号","隔开,最简单的形式{"name":"json"},当有多个key/value值时,只须用英文逗号隔开就可以,{"name":"json","sex":"male"}。我们也可以通过JSON的方式来创建JavaScript对象,例如下面创建的ride对象:
var ride={ make:'yamaha', model:'demo', year:2005, owner:{ name:'yue', occupation:'worker' } }
图 1.1
1.2使用JSON语法创建数组
如图1.2所示,如普通数组一样,它的基本格式是
['arr1','arr2','arr3'];
图 1.2
它的值可以是双引号括起来的字符串(String)、数值(number)、true、false、 null、对象(object)或者数组(array),如图1.3所示。
图 1.3
2 Java解析和构造JSON数据
2.1 所需要的依赖包
解析和构造JSON对象需要用到以下的jar文件:
2.2 Java的基本实现
Employee类:public class Employee { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }下面是关于JSON的一些基本操作
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class demo { public static void main(String[] args) { // TODO Auto-generated method stub // 创建JSON对象 JSONObject json = new JSONObject(); // 创建两个Map对象和一个List对象 Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", "little"); map1.put("sex", "male"); map1.put("age", "23"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("name", "big"); map2.put("sex", "male"); map2.put("age", "28"); List<Map> list = new ArrayList<Map>(); list.add(map1); list.add(map2); // 创建一个Employee对象 Employee em = new Employee(); em.setName("little"); em.setAge(23); // 将Map转换成JSON数据 JSONArray ja1 = JSONArray.fromObject(map1); // 将 List转换成JSON数据 JSONArray ja2 = JSONArray.fromObject(list); // 将JavaBean转换成JSON数据 JSONArray ja4 = JSONArray.fromObject(em); // 数组转换成JSON数据 String[] str = { "sk", "sk1", "sk2" }; JSONArray ja3 = JSONArray.fromObject(str); System.out.println(ja1); System.out.println(ja2); System.out.println(ja3); System.out.println(ja4); // 构造JSON数据 json.put("map", ja1); json.put("employee", ja2); System.out.println(json); // 解析JSON数据 JSONArray ja = json.getJSONArray("employee"); for (int j = 0; j < ja.size(); j++) { System.out.println(((JSONObject) ja2.get(j)).get("name") + "," + ((JSONObject) ja2.get(j)).get("sex") + "," + ((JSONObject) ja2.get(j)).get("age")); } } }
输出结果
[{"sex":"male","age":"23","name":"little"}]
[{"sex":"male","age":"23","name":"little"},{"sex":"male","age":"28","name":"big"}]
["sk","sk1","sk2"]
[{"age":23,"name":"little"}]
{"map":[{"sex":"male","age":"23","name":"little"}],"employee":[{"sex":"male","age":"23","name":"little"},{"sex":"male","age":"28","name":"big"}]}
little,male,23
big,male,28
[{"sex":"male","age":"23","name":"little"},{"sex":"male","age":"28","name":"big"}]
["sk","sk1","sk2"]
[{"age":23,"name":"little"}]
{"map":[{"sex":"male","age":"23","name":"little"}],"employee":[{"sex":"male","age":"23","name":"little"},{"sex":"male","age":"28","name":"big"}]}
little,male,23
big,male,28