zoukankan      html  css  js  c++  java
  • gson将JSON字符串转成Java对象

    在Java中,将一个json格式的字符串转换为一个java类的对象。看到网上很多方法,觉得使用google的Gson的最好用。

    下面一个servlet的httpRequset的处理举例:



    package com.xiyou.webService;

    import java.io.IOException;
    import java.io.PrintWriter;

    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 com.xiyou.util.FormatDateTime;

    import com.google.gson.Gson;

    class TransactionResponse {
        String action, loginAccount, createdInfoDetail, positionInfo;
    }

    public class UserAccountServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        public UserAccountServlet() {
            super();
        }

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

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

            UserAccountController userAccountController = new UserAccountController();
            String result = "";

            String jsonString =
                    "{'action':'add','loginAccount':'137665123333','createdInfoDetail':'createdInfoDetail','positionInfo':'abeddafdafdadafafdfdasfadf'}";

            try {
                jsonString = request.getParameter("json");

                response.setContentType("text/html;charset=utf-8");
                response.setCharacterEncoding("utf-8");

                PrintWriter out = response.getWriter();

                Gson gson = new Gson();
                TransactionResponse tResponse = gson.fromJson(jsonString, TransactionResponse.class);
                result = tResponse.action;
                out.println(result);

                result = tResponse.loginAccount;
                out.println(result);

                result = tResponse.createdInfoDetail;
                out.println(result);

                result = tResponse.positionInfo;
                out.println(result);

                out.flush();
                out.close();

            } catch (Exception e) {
                response.setContentType("text/html;charset=utf-8");
                response.setCharacterEncoding("utf-8");
                PrintWriter out = response.getWriter();
                out.println("但愿朝阳常照我土,莫忘烈士鲜血满地 " + "@" + FormatDateTime.toLocalLongDateByNow());

                out.flush();
                out.close();
                e.printStackTrace();

            }

        }// json get the end of json

    }

  • 相关阅读:
    常见的代码报错信息总结(持续更新ing)
    ASCII码对照表
    python ord()与chr()用法以及区别
    Python random模块sample、randint、shuffle、choice随机函数
    日志相关
    tensorflow学习笔记
    tar 解压缩命令详解
    pandas使用
    相似度与距离计算python代码实现
    逻辑回归原理(python代码实现)
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3119947.html
Copyright © 2011-2022 走看看