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

    }

  • 相关阅读:
    Eclipse常用插件推荐
    Open Source Search Engines in Java
    Java: convert a file to a byte array, then convert byte array to a file.
    常用的Eclipse插件介绍
    一个搜索引擎周边的blog
    java文件读取。(单字节读取和按行读取读取)
    im4java
    csv格式读取通用类
    java以流方式下载文件struts2.x版_心灵的港湾_百度空间
    Jetty/Feature/Jetty Maven Plugin
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3119947.html
Copyright © 2011-2022 走看看