码迷,mamicode.com
首页 > Web开发 > 详细

JSONArray和JSONObject的简单使用

时间:2017-06-14 21:08:02      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:传输数据   hash   为什么   前台   字符   nic   依赖   json对象   字符串   

一、为什么要使用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", "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());
	}
	//依据接收的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
	}
	
	@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"]}
	}
	
}

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


JSONArray和JSONObject的简单使用

标签:传输数据   hash   为什么   前台   字符   nic   依赖   json对象   字符串   

原文地址:http://www.cnblogs.com/yfceshi/p/7010757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!