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

    }

  • 相关阅读:
    数论模板
    acdream1116 Gao the string!(扩展KMP)
    后缀数组模板
    Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
    acdream1116 Gao the string!(hash二分 or 后缀数组)
    SPOJ375 Query on a tree(LCT边权)
    krpano HTML5 Viewer可以实现全景展示
    Chrome 前端 插件
    canvas 星空插件
    jQuery插件开发
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3119947.html
Copyright © 2011-2022 走看看