AjaxAndJson

# AJAX #(异步)

  1. ASynchronous JavaScript And XML
  2. 客户端和服务器端的同步和异步
  3. 提升用户的体验
  4. 是一种在无需加载整个页面的情况下,能够更新部分网页的技术
  5. 通过后台与服务器进行少量的数据交换,Ajax可以使网页实现异步更新,(可以在不重新加载整个页面的情况下, 对网页的某部分进行更新)

    原生js实现


    `



Title



    
    

    

    
    
    `

jQuery实现

  1. 三种实现方式
    1. $.ajax()

    `


    
Title












`
2. $.get()

    `
    
    
        
Title












`

3. $.post()

    `


    
    Title
    
    










`

## Json数据格式 ##

  1. 全称是:JavaScript Object Notation---JavaScript对象表示法
  2. Json现在多用于存储和交换信息的语法--进行数据的传输
  3. Json比XML更小,更快,更容易解析
  4. Json的语法

  5. 规则:

    1. 键值对 key 可以使用引号单双多是可以的, 也可以不用引号
    2. 数据之间用逗号分开
    3. 花括号保存对象(可以是数字,字符串(用引号引起来), 逻辑值(boolean), 数组(用花括号), Json对象,null值)
    4. 方括号用来保存数组

`    `

2.Json获取数据


1.Json对象.键名
2. Json对象[键名]
----------

    `//定义的基本格式
        var Person = {name: "zhangsan", age: "12", gender: "男"}
        var name = Person.name;
        var age = Person["age"];
        var gender = Person['gender'];

        alert(name+age+gender)`

 var Persons = {
            person: [
                {name: "lisi", age: "113", gender: "男"},
                {name: "lisi", age: "113", gender: "男"},
                {name: "lisi", age: "113", gender: "男"},
                {name: "lisi", age: "113", gender: "男"},
            ]
        }
        var name1 = Persons.person[1].name;
        var age1 = Persons.person[1].age;
        var gender1 = Persons.person[1].gender;
        alert(name1+age1+gender1);

## Json的遍历 ##

     //定义的基本格式
    var Person = {name: "zhangsan", age: "12", gender: "男"}
    //遍历Json的key和value
    for(var key in Person){
        //key是一个字符串, 所以不能使用Person.key
        alert(key + Person[key])
    }

` //嵌套格式:数组里面有多个对象
    var ps = [
        {name: "lisi", age: "113", gender: "男"},
        {name: "lisi", age: "113", gender: "男"},
        {name: "lisi", age: "113", gender: "男"},
        {name: "lisi", age: "113", gender: "男"}
    ]
    for (var i = 0; i < ps.length; i++) {
        var p= ps[i]
       for(var key in p){
           alert(key+p[key])
       }

    }`

Json解析器

Jsonlib Gson fastJson (Alibaba)  jackson(springmvc 中的解析器)

  1. Json数据和java对象之间转化

1. json转换java

@Test
public void test04() throws IOException {
    //将json数据转换成java对象
    //1。初始化json数据
    String json="{\"gender\":\"男\",\"name\":\"zhangsan\",\"age\":12}";
    //创建ObjectMapper对象
    ObjectMapper mapper = new ObjectMapper();
    Person person = mapper.readValue(json, Person.class);
    System.out.println(person);
}

@Test
public void test05() throws IOException {//将json字符串转换为List集合
    //1。初始化json数据
    ArrayList list = new ArrayList<>();
    list.add(new Person("zhangsan", 1212, "nan", new Date()));
    list.add(new Person("zhangsan", 1212, "nan", new Date()));
    list.add(new Person("zhangsan", 1212, "nan", new Date()));
    ObjectMapper mapper = new ObjectMapper();
    String s = mapper.writeValueAsString(list);
    //创建ObjectMapper对象
    ArrayList arrayList = mapper.readValue(s, ArrayList.class);
    System.out.println(arrayList);
}

  @Test
public void test07() throws IOException {
    //json字符串转换成HashMap集合
    Map map = new HashMap<>();
    map.put("name","zhangsan");
    map.put("age",12);
    map.put("gender","男");
    ObjectMapper mapper = new ObjectMapper();
    String s = mapper.writeValueAsString(map);
    //{"gender":"男","name":"zhangsan","age":12}
    HashMap hashMap = mapper.readValue(s, HashMap.class);
    Set set = hashMap.entrySet();
    set.stream().forEach(name-> System.out.println(name));
}

2. java转换成json
    1. 导入jar包jackson
    2. 创建jackson对象ObjectMapper
    3. 使用方法转换成Json字符串

# java转换Json字符串 #

    ` @Test
public void test1() throws IOException { //java对象转化成json字符串
    Person person = new Person("zhangsan", 1212, "nan");
    //创建Jackson的核心对象
    ObjectMapper mapper = new ObjectMapper();
    /*
    * mapper.writeValue()
    * 参数:
    * File:将对象转换为json字符串, 并保存到指定的文件中
    * Writer:将对象转换为json字符串,并将json数据填充到字符输出流中
    * OutputStream:将对象转换为json对象, 并将json数据填充到字节输出流中
    *
    * */
    String json = mapper.writeValueAsString(person);
    System.out.println(json);//{"name":"zhangsan","age":1212,"gender":"nan"}
    mapper.writeValue(new File("a.txt"),person);


}`

注解

1. 排除属性的 @JsonIgnore:排除属性(忽略该属性 ,在属性或者getset方法中 使用该注解)
2. @JsonFormat:格式化注解:(使用注解格式化, 用在属性或者)
    @JsonFormat(pattern="yyyy-MM-DD")
    @JsonIgnore
    private Date birthday;**
3.list或者map类型转换成Json

@Test
public void test02() throws JsonProcessingException {
    ArrayList list = new ArrayList<>();
    list.add(new Person("zhangsan", 1212, "nan", new Date()));
    list.add(new Person("zhangsan", 1212, "nan", new Date()));
    list.add(new Person("zhangsan", 1212, "nan", new Date()));
    ObjectMapper mapper = new ObjectMapper();
    String s = mapper.writeValueAsString(list);
    //[{"name":"zhangsan","age":1212,"gender":"nan"},{"name":"zhangsan","age":1212,"gender":"nan"},{"name":"zhangsan","age":1212,"gender":"nan"}]

System.out.println(s);

}
@Test
public void test03() throws JsonProcessingException {
    Map map = new HashMap<>();
    map.put("name","zhangsan");
    map.put("age",12);
    map.put("gender","男");
    ObjectMapper mapper = new ObjectMapper();
    String s = mapper.writeValueAsString(map);
    //{"gender":"男","name":"zhangsan","age":12}
    System.out.println(s);

}

你可能感兴趣的:(Java,ee,开发)