JSONArray和JSONObject的简单使用

前端之家收集整理的这篇文章主要介绍了JSONArray和JSONObject的简单使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、为什么要使用JSONArray和JSONObject

1、后台 --》前台 可以把java对象和集合转化成json字符串格式,这样在前台的ajax方法中可以直接转化成json对象使用 ,从后台前台传值

2、前台 --》使用JSON.stringify(mdses)方法把js对象,数组,转化成json字符串 ,后台使用JSONArray或JSONObject 转化成 java对象或集合

有助于前端参数传递到后端,和后端取值y

3、json现在以成为大多数据传输的载体

二、后台解析前台ajax提交数据,使用较少一般都把list 或map转化为json形式的字符串传值到前台的ajax中:

	//根据接收的json字符串来解析字符串中所包含的数据和数据对象
	@Test
	public void parsJsonFromFront(){
		//接收到的json
		String result = "[{\"username\": \"your name\",\"user_json\": {\"username\": \"your name\",\"nickname\": \"your nickname\"}}]";
	 	//根据字符串陈
		//根据字符串生成JSON对象
		//如果是数组的形式 就使用 JSONArray.fromObject(result) 转化为JSONArray 对象
		//如果是json对象  就是使用JSONObject.fromObject(result) 转化为JSONObject 对象
		JSONArray resultArray = JSONArray.fromObject(result);
		JSONObject jsonObject = resultArray.getJSONObject(0);
		JSONObject user_json = jsonObject.getJSONObject("user_json");
		//根据key值取值
		String username = user_json.getString("username");
		String nickname = user_json.getString("nickname");
		System.out.println("username =" + username  + ":" + "nickname = " + nickname);
		//console :  username =your name:nickname = your nickname
	}

三 、后台转化list 或map转化为json形式的字符串传值到前台的ajax中

user bean

public class User {
	private String name;
	private Adress adress;
	private List<String> students;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Adress getAdress() {
		return adress;
	}
	public void setAdress(Adress adress) {
		this.adress = adress;
	}
	public List<String> getStudents() {
		return students;
	}
	public void setStudents(List<String> students) {
		this.students = students;
	}
	public User(String name,Adress adress,List<String> students) {
		super();
		this.name = name;
		this.adress = adress;
		this.students = students;
	}
	public User() {
		super();
	}
	
}
public class Adress {
	private String streetName;
	private String district;
	public String getStreetName() {
		return streetName;
	}
	public void setStreetName(String streetName) {
		this.streetName = streetName;
	}
	public String getDistrict() {
		return district;
	}
	public void setDistrict(String district) {
		this.district = district;
	}
	public Adress(String streetName,String district) {
		super();
		this.streetName = streetName;
		this.district = district;
	}
	public Adress() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

1)bean to json

@Test
	public void beanToJson(){
		List list = new ArrayList();
		list.add( "xiaowang" );
		list.add( "xiaohua" );
		Adress adress =  new Adress("streetName","district");
		JSONObject jsonObject =  JSONObject.fromObject(new User("name",adress,list));
		System.out.println(jsonObject.toString());
		//console :{"adress":{"district":"district","streetName":"streetName"},"name":"name","students":["xiaowang","xiaohua"]}
	}

2)

	@Test
	public void list2Json(){
		List list = new ArrayList();
		list.add( "first" );
		list.add( "second" );
		JSONArray  jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
	}
	
	public Map getMap(){
		 //声明一个Hash对象并添加数据
		 Map params =  new HashMap();
	
		params.put("username","username");
		params.put("user_json","user");
		return params;
	}
	@Test
	public void map2Json(){
		Map map = getMap();
		Map map1 = getMap();
		List list = new ArrayList();
		list.add(map);
		list.add(map1);
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println("jsonArray--->" + jsonArray.toString());
		JSONArray jsonArray1 = JSONArray.fromObject(map);
		System.out.println("jsonArray1--->" + jsonArray1.toString());
	}

完整:

public class SimpleExample {
	
	@Test
	public void list2Json(){
		List list = new ArrayList();
		list.add( "first" );
		list.add( "second" );
		JSONArray  jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
	}
	
	public Map getMap(){
		 //声明一个Hash对象并添加数据
		 Map params =  new HashMap();
	
		params.put("username","user");
		return params;
	}
	@Test
	public void map2Json(){
		Map map = getMap();
		Map map1 = getMap();
		List list = new ArrayList();
		list.add(map);
		list.add(map1);
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println("jsonArray--->" + jsonArray.toString());
		JSONArray jsonArray1 = JSONArray.fromObject(map);
		System.out.println("jsonArray1--->" + jsonArray1.toString());
	}
	//根据接收的json字符串来解析字符串中所包含的数据和数据对象
	@Test
	public void parsJsonFromFront(){
		//接收到的json
		String result = "[{\"username\": \"your name\",\"nickname\": \"your nickname\"}}]";
	 	//根据字符串陈
		//根据字符串生成JSON对象
		//如果是数组的形式 就使用 JSONArray.fromObject(result) 转化为JSONArray 对象
		//如果是json对象  就是使用JSONObject.fromObject(result) 转化为JSONObject 对象
		JSONArray resultArray = JSONArray.fromObject(result);
		JSONObject jsonObject = resultArray.getJSONObject(0);
		JSONObject user_json = jsonObject.getJSONObject("user_json");
		//根据key值取值
		String username = user_json.getString("username");
		String nickname = user_json.getString("nickname");
		System.out.println("username =" + username  + ":" + "nickname = " + nickname);
		//console :  username =your name:nickname = your nickname
	}
	
	@Test
	public void beanToJson(){
		List list = new ArrayList();
		list.add( "xiaowang" );
		list.add( "xiaohua" );
		Adress adress =  new Adress("streetName","xiaohua"]}
	}
	
}

相关依赖jar下载:http://download.csdn.net/detail/hao007cn/8995465

猜你在找的Json相关文章