日期:2014-05-16  浏览次数:20341 次

jQuery_基础4_Ajax_JSON_XML
<script type="text/javascript">

		var value ={section:
	{
		"tilt":"book-siingning event", 
		"singnig":
			[
				{
					"autor":{"title":"mr,name:vikram"},
					"book":{"tilte":"a suitable boy ,price:22.95"}
				},{
					"autor":{"title":"mr,name:vikram"},
					"book":{"tilte":"a suitable boy ,price:22.95"}
				}
			
			]
	}
};
		
		alert(value.section.tilt);
		alert(value.section.singnig[0].autor.title);
		alert(value.section.singnig[1].book.tilte);
		
</script>


JSON:
1.json中双引号等要转义
2.json uncode编码(全球最大的字符集)

JSON->Java对象  
Java对象->JSON
public static void main(String[] args) throws Exception
	{
		//要传一个格式良好的json字符串
		String jsonContent = "{'hello': 'world', 'abc': 'xyz'}";
		
		//JSONObject:专门处理普通json字符串
		JSONObject jsonObject = new JSONObject(jsonContent);
		
		String str1 = jsonObject.getString("hello");
		String str2 = jsonObject.getString("abc");
		
		System.out.println(str1);
		System.out.println(str2);
		
		System.out.println("--------------------");
		
		//要符合json格式的字符串.  数组的json格式:[:开头 ,   ]:结束
		jsonContent = "[{'hello': 333, 'abc': false, 'xyz': {'a': 1, 'b': 'ab'}}, {'hello': 555, 'abc': true, 'xyz': {'a': 3, 'b': 'ba'}}]";
		
		//JSONArray:专门处理数组的.
		JSONArray jsonArray = new JSONArray(jsonContent);
		
		for(int i = 0; i < jsonArray.length(); i++)
		{
			JSONObject jsonObject2 = jsonArray.getJSONObject(i);
			
			int value1 = jsonObject2.getInt("hello");
			boolean value2 = jsonObject2.getBoolean("abc");
			//String value3 = jsonObject2.getString("xyz");
			
			JSONObject jsonObject3 = jsonObject2.getJSONObject("xyz");
			
			int value3 = jsonObject3.getInt("a");
			String value4 = jsonObject3.getString("b");
			
			System.out.println(value1);
			System.out.println(value2);
			System.out.println(value3);
			System.out.println(value4);
		}
	}


public static void main(String[] args) {
		Person person = new Person();

		person.setB(false);
		person.setUsername("zhangsan");
		person.setPassword("123456");
		person.setAddress(null);
		person.setAge(30);

		person.getList().add("hello");
		person.getList().add("world");
		person.getList().add("hello world");

		Gson gson = new Gson();

		String result = gson.toJson(person);

		System.out.println(result);
		Person person2 = gson.fromJson(result, Person.class);
	}



JSON向前端输出:
		Gson gson = new Gson();
		// 转换之后两个数组
		String result = gson.toJson(list);

		System.out.println(result);

		resp.setContentType("application/json; charset=utf-8");
		resp.setHeader("pragma", "no-cache");
		resp.setHeader("cache-control", "no-cache");
		PrintWriter out = resp.getWriter();
		out.println(result);
		out.flush();

字符串向前端输出:
	int p1 = Integer.parseInt(req.getParameter("param1"));
		int p2 = Integer.parseInt(req.getParameter("param2"));

		resp.setHeader("pragma", "no-cache");
		resp.setHeader("cache-control", "no-cache");
		PrintWriter out = resp.getWriter();
		out.println(p1 + p2);
		out.flush();


XML向前端输出:
// 1 拼接字符串
		// 2 自己生成xml
		Document document = DocumentHelper.createDocument();
		Element rootElement = document.addElement("users");
		rootElement.addComment("This is a comment!");
		Element userElement = rootElement.addElement("user");

		Element idElement = userElement.addElement("id");
		Element nameElement = userElement.addElement("username");
		Element ageElement = userElement.addElement("age");
		Element addressElement = userElement.addElement("address");

		idElement.setText(person.getId() + "");
		nameElement.setText(person.getUsername());
		ageElement.setText(person.getAge() + "");
		addressElement.setText(person.getAddress());

		response.setContentType("text/xml; charset=utf-8");// 设置响应头.
		response.set