我是靠谱客的博主 潇洒裙子,最近开发中收集的这篇文章主要介绍使用fastjson对JSON、实体类转换,实体类转换成JSON字符串,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

JAVABean


public class User {
private Integer userid;
private String username;
private String password;
private String address;
private String email;
private String head_portrait;
private String Phone;
public User(Integer userid, String username, String password, String address, String email, String head_portrait,
String phone) {
super();
this.userid = userid;
this.username = username;
this.password = password;
this.address = address;
this.email = email;
this.head_portrait = head_portrait;
Phone = phone;
}
public User() {
super();
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHead_portrait() {
return head_portrait;
}
public void setHead_portrait(String head_portrait) {
this.head_portrait = head_portrait;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
@Override
public String toString() {
return "{"userid":"" + userid + "", "username":"" + username + "", "password":"" + password
+ "", "address":"" + address + "", "email":"" + email + "", "head_portrait":""
+ head_portrait + "", "Phone":"" + Phone + ""}";
}
}

Controller层

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ds.digitalshop.entity.User;
@Controller
public class TestJson {
@RequestMapping(value="testjson", method = RequestMethod.POST)
public void testjson(HttpServletRequest request,HttpServletResponse response,@RequestBody String json)throws IOException,ServletException{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("原始数据==========" + json);
// 转化为User实体类
User user=JSON.parseObject(json,User.class);
System.out.println("转换为teacher实体类==========" + user.toString());
//Java对象转换成JSON字符串
response.getWriter().write(JSONObject.toJSONString(user));
}
}

JSP代码

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSON测试</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
function SendJson() {
//将数据转换为JSON串
var obj = JSON.stringify({"userid":2,"username":"张三","password":"123","address":"广西南宁","email":"939296345@qq.com","head_portrait":"1null","phone":"18376680552"});
$.ajax({
url:"testjson", //请求的url地址
dataType:"text", //返回格式为json
async:true,//请求是否异步,默认为异步,这也是ajax重要特性
data:obj, //参数值
type:"POST", //请求方式
contentType: 'application/json;charset=utf-8',
success:function(data){
//获取响应内容
//获取元素对象	处理响应内容
console.log("数据对象:"+data);
alert(data)
}
});
}
</script>
</style>
</head>
<body>
<h3>欢迎光临</h3>
<hr>
<input type="button" value="发送json数据测试" onclick="SendJson()" />
<hr>
</body>
</html>

原生ajax的创建及其状态码

在这里插入图片描述

状态码
在这里插入图片描述

最后

以上就是潇洒裙子为你收集整理的使用fastjson对JSON、实体类转换,实体类转换成JSON字符串的全部内容,希望文章能够帮你解决使用fastjson对JSON、实体类转换,实体类转换成JSON字符串所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部