其他分享
首页 > 其他分享> > Json将字符串转换成对象输出

Json将字符串转换成对象输出

作者:互联网

Json将字符串转换成对象输出

<%--
  Created by IntelliJ IDEA.
  User: tomel
  Date: 2022/5/31
  Time: 14:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        var xhr;

        function  getData(){
            //获取一个 XMLHttpRequest对象 ,对象可以帮助我们发送异步请求
            xhr =new XMLHttpRequest();
            // 使用xhr对象设置打开链接,设置请求方式和参数xhr.open("请求方式","请求的URL",是否使用异步方式);
            xhr.open("GET","testDataServlet.do",true)

            xhr.onreadystatechange=showData

            xhr.send(null)

        }

        function showData(){

            if(xhr.readyState==4&&xhr.status==200) {
                var text =xhr.responseText;
                var users= JSON.parse(text)
                for(var i=0;i<users.length;i++){
                    var user = users[i];
                    console.info(user.name)
                    console.log(user.age);
                    console.log(user.gender)
                }



            }

        }
    </script>
</head>
<body>
<button type="button" onclick="getData()">触发</button>
</body>
</html>

package com.msb;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.GenericFilter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.channels.NonWritableChannelException;
import java.sql.Array;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;

@WebServlet("/testDataServlet.do")
public class testDataServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                                                            //import java.sql.Date;:"一月 1, 1970
        User user = new User("小胖",23,'男', Date.valueOf("2018-8-14"));
        User user2 = new User("大胖",23,'男', Date.valueOf("2020-8-14"));
        User user3 = new User("中胖",23,'男', Date.valueOf("2021-8-14"));
        ArrayList<Object> lists = new ArrayList<>();
        Collections.addAll(lists,user,user2,user3);
        //Gson gson = new Gson();
        GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
        Gson gson = gsonBuilder.create();
        String sql = gson.toJson(lists);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().println(sql);
        /*resp.getWriter().println(sql);*/

    }
}

package com.msb;

import java.sql.Date;

public class User {
    private String name;
    private int age;
    private char gender;
    private Date birthday;

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                ", birthday=" + birthday +
                '}';
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public User(String name, int age, char gender, Date birthday) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.birthday = birthday;
    }
}

标签:转换成,name,gender,Date,Json,User,字符串,import,public
来源: https://www.cnblogs.com/188221creat/p/16331084.html