JSON

google Gson学习笔记及与json

字号+ 作者:H5之家 来源:H5之家 2017-10-02 16:05 我要评论( )

因为gson网上的帮助文档打开时比较慢,所以把帮助文档摘录如此,方便查看: 1. 基本类型转化 Java代码 public static void main(String[] args) { Gson gson = new Gson(); System.out.println(gson.toJson(1)); // == prints 1 System.out.println(gson.toJson

因为gson网上的帮助文档打开时比较慢,所以把帮助文档摘录如此,方便查看:

1. 基本类型转化

Java代码 

public static void main(String[] args) {  

        Gson gson = new Gson();  

        System.out.println(gson.toJson(1)); // ==> prints 1  

        System.out.println(gson.toJson("abcd"));// ==> prints "abcd"  

        System.out.println(gson.toJson(new Long(10)));// ==> prints 10  

        int[] values = { 1 };  

        System.out.println(gson.toJson(values));// ==> prints [1]  

        System.out.println("============");  

  

        int one = gson.fromJson("1", int.class);  

        Integer one1 = gson.fromJson("1", Integer.class);  

        Long one2 = gson.fromJson("1", Long.class);  

        String str = gson.fromJson("\"abc\"", String.class);  

        String anotherStr = gson.fromJson("[\"abc\"]", String.class);  

        int[] ints = gson.fromJson("[1,2,3,4,5]", int[].class);  

        Boolean b = gson.fromJson("false", Boolean.class);  

        System.out.println(b == false); //==> prints true  

    }  

 2.对象转化

Java代码 

public class BagOfPrimitives {  

  

    private int           value1    = 1;  

    private String        value2    = "abc";  

//是用于声明变量在序列化的时候不被存储  

    private transient int   value3  = 3;  

  

    BagOfPrimitives() {  

        // no-args constructor  

    }  

  

    public static void main(String[] args) {  

        BagOfPrimitives obj = new BagOfPrimitives();  

        Gson gson = new Gson();  

        String json = gson.toJson(obj);    

        System.out.println(json); //==> json is {"value1":1,"value2":"abc"}  

          

        BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   

        System.out.println(obj2.value1);  

        System.out.println(obj2.value2);  

        System.out.println(obj2.value3);//==>3  

          

        String json1 = "{'value1':1,'value2':'abc','value3':4}";  

        BagOfPrimitives obj3 = gson.fromJson(json1, BagOfPrimitives.class);   

        System.out.println(obj3.value1);  

        System.out.println(obj3.value2);  

        System.out.println(obj3.value3); //==>3  

    }  

}  

Note that you can not serialize objects with circular references since that will result in infinite recursion. 

如果要是用json lib的话,上面的代码可以写成如下所示:

Java代码 

String json1 = "{'value1':1,'value2':'abc','value3':4}";  

JSONObject jsonObj = JSONObject.fromObject( json1 );   

BagOfPrimitives obj3 = (BagOfPrimitives) JSONObject.toBean( jsonObj, BagOfPrimitives.class );  

 Finer Points with Objects

It is perfectly fine (and recommended) to use private fields 

There is no need to use any annotations to indicate a field is to be included for serialization and deserialization. All fields in the current class (and from all super classes) are included by default. 

If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.

This implementation handles nulls correctly

While serialization, a null field is skipped from the output

While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null

If a field is synthetic , it is ignored and not included in JSON serialization or deserialization

Fields corresponding to the outer classes in  inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization

Nested Classes (including Inner Classes)

Gson can serialize static nested classes quite easily. 


Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it. Here is an example: 

Java代码 

public class A {  

  public String a;  

  

  class B {  

  

    public String b;  

  

    public B() {  

      // No args constructor for B  

    }  

  }  

}  

 NOTE : The above class B can not (by default) be serialized with Gson.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 关于JSONObject与GSON解析JSON数据详解

    关于JSONObject与GSON解析JSON数据详解

    2017-10-02 10:02

  • JSON基础学习

    JSON基础学习

    2017-10-01 17:06

  • Json.Net学习 异常处理

    Json.Net学习 异常处理

    2017-10-01 14:02

  • Python学习者

    Python学习者

    2017-09-29 16:01

网友点评