• 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

    }

  • 相关阅读:
    nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】
    转 :hdoj 4857 逃生【反向拓扑】
    hdoj 3342 Legal or Not【拓扑排序】
    hdoj 2094 产生冠军
    poj 1789 Truck History【最小生成树prime】
    转:【拓扑排序详解】+【模板】
    hdoj 1285 确定比赛名次【拓扑排序】
    poj 2031 Building a Space Station【最小生成树prime】【模板题】
    zzuoj 10408: C.最少换乘【最短路dijkstra】
    [LC] 232. Implement Queue using Stacks
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3119947.html
Copyright © 2020-2023  润新知