zoukankan      html  css  js  c++  java
  • Json与Gson

    JSON官网:http://json.org/json-zh.html
    Gson:在github:https://github.com/google/gson

    JSONOBejct缺点:
    1从javaBean能转化为jsonObject,却不能反解析.
    2JsonObect没有日期类型,因此javaBean中日期类型只能折中的写成String类型

    Gson的优点就是解决了Json的缺点

    特点:
    JSON是Android SDK官方的库,适合做移动端

    GSON适用于服务端开发

    GSON的功能比json的JSONObject功能更加强大

     File file = new File(ReadJSONSample.class.getResource("/OSCAR.json").getFile());
            String content = FileUtils.readFileToString(file,"utf-8");

            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();                     
            DiaosiWithBirthday OSCAR = gson.fromJson(content,DiaosiWithBirthday.class); //json反向解析为Bean
            System.out.println(OSCAR.getBirthday().toLocaleString());

            //集合类解析
            System.out.println(OSCAR.getMajor());
            System.out.println(OSCAR.getMajor().getClass());
    4//下面三行代码可以进行美化,不会再同一行输出
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setPrettyPrinting();
            gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
                @Override
                public String translateName(Field f) {//自定义回调函数

                    if (f.getName().equals("name")){
                        return "NAME";
                    }
                    return f.getName();
                }
            });
            Gson gson = gsonBuilder.create();
            //Gson gson = new Gson();
           // System.out.println(gson.toJson(OSCAR));

    还有JSONObject在项目里未拷贝,因为GSON的功能要强大!!!!!!!!!!!!1
  • 相关阅读:
    CF1051D Bicolorings dp
    loj2480 [CEOI2017]One-Way Streets 边双+树上差分
    有趣的支配树
    AtCoder Regular Contest 81
    [BZOJ5305][HAOI2018]苹果树(DP)
    [BZOJ4699]树上的最短路(最短路+线段树)
    [BZOJ3507][CQOI2014]通配符匹配(DP+Hash)
    [Luogu4724][模板]三维凸包(增量构造法)
    [BZOJ5317][JSOI2018]部落战争(闵可夫斯基和)
    [WC2014]时空穿梭(莫比乌斯反演)
  • 原文地址:https://www.cnblogs.com/xinglongbing521/p/10383195.html
Copyright © 2011-2022 走看看