Json in Java 的使用

1 json 就是一种数据的组拼形式,举个例子:

{"mine":[{"name":"name0","time":"time0"},{"name":"name1","time":"time1"},{"name":"name2","time":"time2"},{"name":"name3","time":"time3"},{"name":"name4","time":"time4"},{"name":"name5","time":"time5"},{"name":"name6","time":"time6"},{"name":"name7","time":"time7"},{"name":"name8","time":"time8"},{"name":"name9","time":"time9"}],"others":[{"name":"name0"},{"name":"name1"},{"name":"name2"},{"name":"name3"},{"name":"name4"}]}


上面就是由JsonObject + JsonArray组拼出来的一段数据。

整体结构是{"mine": xxx,"others":xxxx}, 即{jsonObject,jsonObject,...}有两个JsonObject组成。

第一个JsonObject:

"mine":[{"name":"name0","time":"time0"},{"name":"name1","time":"time1"},{"name":"name2","time":"time2"},{"name":"name3","time":"time3"},{"name":"name4","time":"time4"},{"name":"name5","time":"time5"},{"name":"name6","time":"time6"},{"name":"name7","time":"time7"},{"name":"name8","time":"time8"},{"name":"name9","time":"time9"}]

它的key是mine, 它的value是一个JsonArray,呦[]包裹,[]中间放的是一系列的jsonObject,即[jsonObject,jsonObject,.....]

JsonArray的存储形式类似于ListArray,其中存储的对象类型相同,

JsonObject的存储形式类似于Map,有一系列的Key-value组成而value的类型是多种多样的,可以是一个String对象,也可以是一个ArrayList对象


2 如何使用json来完成数据的组拼?

2.1 根据1中介绍的内容,我们完全可以自己组拼出这样一个json格式的数据,。

优点:不需要导入格外的jar包

缺点:很容易出错,标点符号,大括号等等。。

2.2 使用额外的jar包,一共需要六个jar包,分别是:

  1. commons-logging-1.0.4.jar  
  2. commons-lang-2.3.jar  
  3. commons-collections-3.2.jar  
  4. commons-beanutils-1.7.0.jar  
  5. json-lib-2.2.1-jdk15.jar  
  6. ezmorph-1.0.4.jar

3 举例:

3.1 组拼出1的的那一串json数据:

	public static JSONObject setData(){
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		
		Mine mine1 = new Mine("name1", "time1");
		Mine mine2 = new Mine("name2", "time2");
	
		List<Mine> list = new ArrayList<Mine>();
		for(int i=0;i<10;i++){
			Mine mine = new Mine("name"+i, "time"+i);
			list.add(mine);
		}

		for(int i=0;i<list.size();i++){
			Mine mine = list.get(i);
			JSONObject jsonObject2 = new JSONObject();
			jsonObject2.put("name", mine.getName());
			jsonObject2.put("time", mine.getTime());
			jsonArray.add(jsonObject2);
			
		}

		JSONArray jsonArray2 = new JSONArray();
		
		List<Others> list2 = new ArrayList<Others>();
		
		for(int i=0;i<5;i++){
			Others others = new Others("name"+i);
			list2.add(others);
		}
		
		for(int i=0;i<list2.size();i++){
			Others others = list2.get(i);
			JSONObject jsonObject2  = new JSONObject();
			jsonObject2.put("name", others.getName());
			jsonArray2.add(jsonObject2);
		}
		
		jsonObject.put("mine", jsonArray);
		jsonObject.put("others", jsonArray2);
		
		System.out.println(jsonObject.toString());
		
		return jsonObject;
	}

3.2 往往json格式的数据,是用来数据存储和数据传输的,当拿到json格式的数据之后,我们需要将其解析成我们需要的对象。

	public static void getData(JSONObject jsonObject){
		
		JSONArray jsonArray = jsonObject.getJSONArray("mine");
		
		for(int i=0;i<jsonArray.size();i++){
			JSONObject jsonObject2 = jsonArray.getJSONObject(i);
			
			String name = jsonObject2.getString("name");
			String time = jsonObject2.getString("time");
			Mine mine = new Mine(name, time);
			System.out.println(mine.toString());
		}

	}

补充例子中提到的那两个类:

class Mine{
	private String name;
	private String time;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public Mine(String name, String time) {
		super();
		this.name = name;
		this.time = time;
	}
	@Override
	public String toString() {
		return "Mine [name=" + name + ", time=" + time + "]";
	}
	
	
}

class Others{
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Others(String name) {
		super();
		this.name = name;
	}

	@Override
	public String toString() {
		return "Others [name=" + name + "]";
	}
	
}


Json in Java 的使用,布布扣,bubuko.com

Json in Java 的使用

上一篇:Java和PHP哪个方向更有前途?


下一篇:C++类模版学习笔记