java中json的 *** 作

java中json的 *** 作,第1张

java中json的 *** 作

servlet代码

package com.zx.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zx.entity.Clas;
import com.zx.entity.Student;
import com.zx.entity.Teacher;

public class LoginServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("LoginServlet---doGet");
		Gson gson = new Gson();
		//1、java对象转json字符串
		Teacher[] teas = new Teacher[] {new Teacher(11, "张老师"), new Teacher(12, "李老师")};
		Clas clas = new Clas(101, "一年一班");
		Clas clas1 = new Clas(102, "一年二班");
		Student stu = new Student(10, "张三", clas, teas);
		String json_str1 = gson.toJson(stu);
		System.out.println("json字符串1:"+json_str1);
		//1.1、json字符串转java对象
//		gson.fromJson(json_str1, new MyTypeToken().getType());
		//protected,匿名内部类
		Student stu1 = gson.fromJson(json_str1, new TypeToken() {}.getType());
		System.out.println("stu1:"+stu1);
		//2、java对象list集合和json的转换
		List list = new ArrayList<>();
		list.add(clas);
		list.add(clas1);
		String json_str2 = gson.toJson(list);
		System.out.println("json字符串2:"+json_str2);
		//2.1、json字符串转集合
		List list1 = gson.fromJson(json_str2, new TypeToken>() {}.getType());
		System.out.println("list1:"+list1.get(1).toString());
		//3、map 对象和json 的转换
		Map map = new HashMap<>();
		map.put("one", clas);
		map.put("two", clas1);
		String json_str3 = gson.toJson(map);
		System.out.println("json字符串3:"+json_str3);
		//3.1、json字符串转map
		Map map1 = gson.fromJson(json_str3, new TypeToken>(){}.getType());
		System.out.println("map1:"+map1.get("two"));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
//class MyTypeToken extends TypeToken{
//	public MyTypeToken() {
//		super();
//	}
//}
实体类代码:
package com.zx.entity;

public class Clas {
	
	private int classNo;
	private String className;
	public Clas() {
		super();
	}
	public Clas(int classNo, String className) {
		super();
		this.classNo = classNo;
		this.className = className;
	}
	public int getClassNo() {
		return classNo;
	}
	public void setClassNo(int classNo) {
		this.classNo = classNo;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	@Override
	public String toString() {
		return "Clas [classNo=" + classNo + ", className=" + className + "]";
	}
}

package com.zx.entity;

import java.util.Arrays;

public class Student{
	
	private int id;
	private String userName;
	private Clas clas;	//所在班级
	private Teacher[] teas;		//老师们
	
	public Student() {
		super();
	}
	public Student(int id, String userName, Clas clas, Teacher[] teas) {
		super();
		this.id = id;
		this.userName = userName;
		this.clas = clas;
		this.teas = teas;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Clas getClas() {
		return clas;
	}
	public void setClas(Clas clas) {
		this.clas = clas;
	}
	public Teacher[] getTeas() {
		return teas;
	}
	public void setTeas(Teacher[] teas) {
		this.teas = teas;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", userName=" + userName + ", clas=" + clas + ", teas=" + Arrays.toString(teas)
				+ "]";
	}
}

package com.zx.entity;

public class Teacher {
	
	private int id;
	private String name;
	
	public Teacher() {
		super();
	}
	public Teacher(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}
}

前端页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录
	


	


web.xml代码



	javaweb13
	
		loginServlet
		com.zx.servlet.LoginServlet
	
	
		loginServlet
		/login
	

需要jar包:
gson-2.2.4.jar

访问路径:
localhost:8080/javaweb13/login

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5673013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存