1.ajax中数据交换机制
数据转换图
2.
gson组件
Gson gson = new Gson();
String jsonString = gson.toJson(要转为json的对象);
作用:google提供一个专门用于把java对象或者集合转换成JSON字符串的类库
使用的jar包:(在本人的资源库中,可以进行下载使用)
Address address = new Address("bj","100000001");
User u = new User(1,"suns","123456",address);
Gson gson = new Gson();
String jsonString = gson.toJson(u);
System.out.println(jsonString);
结果:{"id":1,"name":"suns","password":"123456","address":{"city":"bj","zipcode":"100000001"}}
3.
$.parseJSON()
作用:把json字符串转换成js对象 js数组
var jsonString = "{\"id\":1,\"name\":\"suns\",\"password\":\"123456\"}";
var user = $.parseJSON(jsonString);
alert(user.id);
实例
1.gson
User:
package com.ajax;
public class User {
private Integer id;
private String name;
private String password;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public User(Integer id, String name, String password,Address address) {
super();
this.id = id;
this.name = name;
this.password = password;
this.address = address;
}
public User() {
super();
}
}
Address:
package com.ajax;
public class Address {
private String city;
private String zipcode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public Address(String city, String zipcode) {
super();
this.city = city;
this.zipcode = zipcode;
}
public Address() {
super();
}
}
TestGson1.java
package com.ajax;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.google.gson.Gson;
public class TestGson1 {
/*@Test
public void test1() {
User u = new User(1,"suns","123456");//json字符
Gson gson = new Gson();
String jsonString = gson.toJson(u);
System.out.println(jsonString);
//运行结果
//{"id":1,"name":"suns","password":"123456"}
}
@Test
public void test2() {
String[] names = new String[]{"suns","huxz"};
Gson gson = new Gson();
String jsonString = gson.toJson(names);
System.out.println(jsonString);
//运行结果
//["suns","huxz"]
}
@Test
public void test3() { // 集合外为综括号,内为大括号
List<User> users = new ArrayList<User>();
User u1 = new User(1,"suns","123456");
User u2 = new User(2,"zhang","123456");
users.add(u1);
users.add(u2);
Gson gson = new Gson();
String jsonString = gson.toJson(users);
System.out.println(jsonString);
//运行结果
//[{"id":1,"name":"suns","password":"123456"},{"id":2,"name":"zhang","password":"123456"}]
}*/
@Test
public void test4() { // 集合外为综括号,内为大括号
Address address = new Address("bj","100000001");
User u = new User(1,"suns","123456",address);
Gson gson = new Gson();
String jsonString = gson.toJson(u);
System.out.println(jsonString);
//运行结果
//{"id":1,"name":"suns","password":"123456","address":{"city":"bj","zipcode":"100000001"}}
}
}
testparseJson.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
//parseJSON 将json对象转化为js对象
var jsonString = "{\"id\":1,\"name\":\"suns\",\"password\":\"123456\"}";
var user = $.parseJSON(jsonString);
alert(user.id);
alert(user.name);
alert(user.password);
var jsonArray = "[\"suns\",\"huxz\"]";
var names = $.parseJSON(jsonArray);
for( var i = 0; i < names.length; i++){
alert(names[i]);
}
</script>
</head>
<body>
</body>
</html>