700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java如何处理前端传来的json格式的数据并将它转化为相应的对象 然后进行使用

Java如何处理前端传来的json格式的数据并将它转化为相应的对象 然后进行使用

时间:2020-02-23 23:22:09

相关推荐

Java如何处理前端传来的json格式的数据并将它转化为相应的对象 然后进行使用

我是小康小白,一个平平无奇的Java小白。热爱有趣的文字,生活和远方。

前端传来json格式的数据传递而来,如何处理json格式的数据,如何将其转化为对象方便进行操作,并在Servlet页面中将数据以对象形式输出

先放个postman测试的结果

Servlet的代码

@WebServlet("/Servlet")public class Servlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 响应参数格式设置response.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");//将此字符串对象输出PrintWriter out=response.getWriter();//使用IOUtils的读取包方式// BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));// String body = IOUtils.read(reader);//使用InputStreamReader对象,获取前端传来的数据.其中// request.getInputStream()是读取前端传递来的数据字节流,// StandardCharsets.UTF_8是将前端传来的数据转化为UTF-8的编码方式InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);StringBuilder body = new StringBuilder();int respInt = insr.read();while(respInt!=-1) { // 读取请求数据//将读取的字节流中的每一个字节转化为字符,然后添加到StringBuilder类型的对象中body.append((char) respInt);respInt = insr.read();}//out的print方法可以输出对象out.print(body);JsonChange jsonChange = new JsonChange();//将StringBuilder类型的对象的对象通过toString方法转化为String类型,然后用fastjson的json包进行转化User user = jsonChange.JsonChangeJavaObject(body.toString());out.write(user.toString());//将int类型的变量转化为String类型String userPrasisepoints = String.valueOf(user.getPrasisepoints());String userPageview = String.valueOf(user.getPageview());SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateString = formatter.format(user.getTime());out.write(user.getUsername()+'\n');out.write(user.getP_headline()+'\n');out.write(user.getP_content()+'\n');out.write( userPrasisepoints);out.write('\n');out.write( userPageview);out.write('\n');out.write(dateString);// out.write(body);}

至于从postman获取json数据,如何进行简单输出可以看我的另一篇博客如何接收前端传来的json数据

但这篇博客中仅仅是实现了从前端获取json数据包,然后进行简单输出,无法获得将数据传递给相应的对象。

这里需要对json数据进行解码。

笔者在这里使用的是fastjson的jar包,下载地址提取码: vqwd。具体代码如下

下面是笔者对应的成员类

package YunNotes.users_publicnotes.entity;import java.util.Date;import com.alibaba.fastjson.*;/*** 此类中包换所有用户信息,包括:username:用户名;* p_headline:公开笔记的标题;* p_content:公开笔记的内容;* prasisepoints:点赞数;* pageview:页面浏览量;* time:笔记的发布时间;* 以及以上类的getter()方法和setter()方法;* 这是主类,包含最多;* @author 10593*/public class User {private String username;private String p_headline;private String p_content;private int prasisepoints;private int pageview;private Date time;public User(){}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getP_headline() {return p_headline;}public void setP_headline(String p_headline) {this.p_headline = p_headline;}public String getP_content() {return p_content;}public void setP_content(String p_content) {this.p_content = p_content;}public int getPrasisepoints() {return prasisepoints;}public void setPrasisepoints(int prasisepoints) {this.prasisepoints = prasisepoints;}public int getPageview() {return pageview;}public void setPageview(int pageview) {this.pageview = pageview;}public Date getTime() {return time;}public void setTime(Date time) {this.time = time;}@Overridepublic String toString() {return "User{" +"username='" + username + '\'' +", p_headline='" + p_headline + '\'' +", p_content='" + p_content + '\'' +", prasisepoints=" + prasisepoints +", pageview=" + pageview +", time=" + time +'}'+'\n';}}

代码中需要注意的地方(笔者之前踩过的坑)

1.如果测试时获取的json格式的数据包进行输出时出现了中文乱码,此时需要修改tomcat的编码方式。

idea的修改方式如下

2.如果日期类型的格式不正确,可以向其中添加下面相应的代码进行修改

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateString = formatter.format(user.getTime());

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。