Day04ajax中數據交換機制(json)串 $.parseJSON,gson.toJSON

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章