Json 和 Gson


json是一种数据格式,便于数据传输、存储、交换
gson是一种组件库,可以把java对象数据转换成json数据格式 

TestJson类
package Test.action;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//用之前先把org.json包里面的类 全部放到项目里
public class TestJson {
	public static void main(String[] args) throws JSONException {
		String str1 = "{name:'wangyafei',age:'20',sex:'man',fav:{1:'money',2:'money too'}}";
		JSONObject jsonObject = new JSONObject(str1);
		System.out.println(jsonObject.get("name"));
		System.out.println(jsonObject.get("age"));
		JSONObject fav = jsonObject.getJSONObject("fav");
		System.out.println(fav.get("1"));
		System.out.println(fav.get("2"));
		jsonObject.put("add", "mafang");
		System.out.println(jsonObject.toString());
		// -----------------------------------------------------------------------------------
		String str2 = "[{name:'wangyafei'},{age:'20',sex:'woman'},{sex:'man'},{fav:'money'}]";
		//強烈注意 放里面的是一个个的对象 这样看来下面那句原来写的话 似乎没有用了
		//每一个大括号内是一个数组的值 每一个值可以放好几个参数,比如下标1的数组和下标2的数组值就不同
		JSONArray array = new JSONArray(str2);
		System.out.println(array.length());
		System.out.println(array.getJSONObject(0).get("name"));
		System.out.println(array.getJSONObject(1).get("sex"));
		//for(int i = 0; i < array.length(); i++){
			//JSONObject object = array.getJSONObject(i);
			//只是说 它是可以遍历的
		//}
		array.put(0, "{name1:'yafei',age:'20',sex:'woman',fav:{aihao1:'money',aihao2:'money too'}}");
		//经过大量实验发现 put之后把当前下标的元素都覆盖了
		System.out.println(array.toString());
	}
}


TestGson类

package Test.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Test.vo.User;

import com.google.gson.Gson;

public class TestGson {
	public static void main(String[] args) {
		
		List fav = new ArrayList();
		fav.add("book");
		fav.add("movie");
		fav.add("music");
		User userA = new User("A","man",fav);
		User userB = new User("B","man",fav);
		User userC = new User("C","woman",fav);
		Gson gson = new Gson();
		System.out.println(gson.toJson(userA));
		
		List arr = new ArrayList();
		arr.add(userA);
		arr.add(userB);
		arr.add(userC);
		Map<String, User> map = new HashMap<String, User>();
		map.put("AA", userA);
		map.put("BB", userB);
		map.put("CC", userC);
		System.out.println(gson.toJson(arr));
		System.out.println(gson.toJson(map));
		
		User userD = gson.fromJson("{name:\"D\",sex:\"man\",fav:[\"book\",\"game\"]}", User.class);
		System.out.println(userD.getName()+"  "+userD.getSex());
	}
}

User类
package Test.vo;

import java.util.ArrayList;
import java.util.List;

public class User {
	String name;
	String sex;
	List fav = new ArrayList();
	public User(String name, String sex, List fav) {
		super();
		this.name = name;
		this.sex = sex;
		this.fav = fav;
	}
	public User() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public List getFav() {
		return fav;
	}
	public void setFav(List fav) {
		this.fav = fav;
	}
	
}

你可能感兴趣的:(json,gson)