该空间是个人实践过程中的总结,欢迎交流吐槽!

【脚本写作】java:将java数据转为Json串-使用Gson

上一篇 / 下一篇  2016-09-08 15:15:30 / 个人分类:Java

package funcLaunDesktop.jsonDataStructure;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 构造json串 - 显示文件内容 - send Request json data
 * { "tp": "client",  #transport protocol
 *   "data": { "action": "view", "path": "/public_html/README.md"  #绝对路径
 *   "startpage": "0", "linage": "30", } }
 * Created by Janesong on 2016/8/29.
 */
public class SendCatFileJson {
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    private final String tp;
    private final JsonData data;

    public SendCatFileJson(String tp, JsonData data) {
        this.tp = tp;
        this.data = data;
    }

    public String toJson() {
        return GSON.toJson(this, SendCatFileJson.class);
    }

    public String getTP() {
        return tp;
    }

    public JsonData getJsonData() {
        return data;
    }

    public static class JsonData {

        private final String action;
        private final String path;
        private final int startPage;
        private final int linage;

        public JsonData(String action, String path, int startPage, int linage) {
            this.action = action;
            this.path = path;
            this.startPage = startPage;
            this.linage = linage;
        }

        public String getAction() {
            return action;
        }

        public String getPath() {
            return path;
        }
    }

    public static void main(String[] args) {
        SendCatFileJson catFileJson = new SendCatFileJson("client", new JsonData("view", "/public_html/README.md", 0, 30));
        System.out.println(catFileJson .toJson());
    }

}


TAG: gson java

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 17814
  • 日志数: 16
  • 建立时间: 2016-09-08
  • 更新时间: 2018-03-28

RSS订阅

Open Toolbar