Gson

https://blog.csdn.net/kakaxi1o1/article/details/79643399

Gson介绍:

GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个Json字符转成一个Java对象,或者将一个Java转化为Json字符串。
特点: a、快速、高效
      b、代码量少、简洁
      c、面向对象
        d、数据传递和解析方便
 
Gson的pom依赖:
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>

Gson的创建方式:

  方式一:

Gson gson = new gson();

  方式二:通过GsonBuilder(),可以配置多种配置。

复制代码
Gson gson = new GsonBuilder()
                        .setLenient()// json宽松  
                        .enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式  
                        .serializeNulls() //智能null  
                        .setPrettyPrinting()// 调教格式  
                        .disableHtmlEscaping() //默认是GSON把HTML 转义的
                        .create();  
复制代码

Gson的基本用法:

注:JavaBean:

复制代码
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
@Builder
public class PersonJson {
    private String name;
    private Integer age;
    private String hobby;
}
//hobby是在后面的例子中添加的。要么就name,age两个属性,要么就三个。
//上面的注解是lombok的注解,起到简化Bean类的作用。
复制代码

Gson提供了public String toJson(Objcet obj)方法,可以将对象转化为json字符串。

JavaBean转化为json字符串

复制代码
public class IndexTest {
    PersonJson person;
    @Before
    public void prepare() {
        person = new PersonJson("栗霖",18);
    }
@Test
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> index() {
    Gson gson </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> Gson();
    System.out.println(gson.toJson(person));
    System.out.println(</span>"---------------"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    Gson gson1 </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> GsonBuilder().create();
    System.out.println(gson1.toJson(person));
}

}

复制代码

结果:

List Map 转化为json字符串

复制代码
public class IndexTest {
PersonJson person;
List<PersonJson> list = new ArrayList<>();
Map<String,PersonJson> map = new HashMap<>();
@Before
public void prepare() {
person = new PersonJson(“栗霖”,18);
list.add(person);
map.put(person.getName(),person);
}
@Test
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> index() {
    Gson gson </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> Gson();
    System.out.println(</span>"----&gt;List convert json" +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> gson.toJson(list));
    System.out.println(</span>"------------------------"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    System.out.println(</span>"----&gt;map convert json" +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> gson.toJson(map));
}

}

复制代码

结果:

 

Gson提供了public T fromJson(String jsonStr,T.class)方法,可以将json字符串转化为Java对象 

json字符串转化为JavaBean

复制代码
public class SecondTest {
@Test
public void index() {
String jsonStr = “{“name”:“栗霖”,“age”:“18”}”;
Gson gson = new GsonBuilder().create();
PersonJson p = gson.fromJson(jsonStr,PersonJson.class);
System.out.println("---->jsonStr convert javaBean " + p.getName() + " " + p.getAge());
}
}
复制代码

结果:

json字符串转化为list集合

复制代码
public class SecondTest {
@Test
public void index() {
String listJsonStr = “[{“name”:“栗霖”,“age”:“18”},{“name”:“栗霖之雨”,“age”:“18”}]”;
Gson gson = new GsonBuilder().create();
List<PersonJson> list = gson.fromJson(listJsonStr,new TypeToken<ArrayList<PersonJson>>(){}.getType());
System.out.println("---->listJsonStr convert List " + list);
}
}
复制代码

结果:

json的抽象基类JsonElemetn:

JsonNull其实就是Null字段

复制代码
public class SecondTest {
@Test
public void index() {
//JsonNull jsonNull = new JsonNull();该方法已经过时
JsonNull jsonNull = JsonNull.INSTANCE;
System.out.println("---->jsonNull " + jsonNull);
}
}
复制代码

结果:

jsonPrimitive可以帮助我们获取带转义字符的字符串。这个就不写了。感觉没啥用到。

创建JsonObject:

  可以通过addProperty(String,Object)向JsonObject添加属性,跟hashmap类似。

复制代码
public class SecondTest {
@Test
public void index() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(“name”,“栗霖”);
jsonObject.addProperty(“age”,18);
System.out.println("---->create jsonObject " + jsonObject);
}
}
复制代码

结果:

创建JsonArray

复制代码
public class SecondTest {
@Test
public void index() {
JsonArray jsonArray = new JsonArray();
jsonArray.add(“a”);
jsonArray.add(“b”);
jsonArray.add(“c”);
jsonArray.add(“d”);
System.out.println("---->create jsonArray: " + jsonArray);
}
}
复制代码

结果:

JsonObject嵌套数组或者说JsonObject嵌套JsonArray

复制代码
public class SecondTest {
@Test
public void index() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(“name”,“栗霖”);
jsonObject.addProperty(“age”,18);
JsonArray jsonArray = new JsonArray();
jsonArray.add(“是码农”);
jsonArray.add(“以前喜欢玩游戏”);
jsonArray.add(“现在只敢小心积累”);
jsonArray.add(“只怕突然白了头”);
jsonObject.add(“status”,jsonArray);
System.out.println("---->create jsonArray: " + jsonObject);
}
}
复制代码

结果:


 

Gson注解

重命名注解:@SerializedName

当你调用其他服务时,他们返回的json KEY值与你的Bean属性名称不对应怎么办?

这时候就需要@SerializedName啦。他可以帮助你解决这个问题!

实体类:

复制代码
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
@Builder
public class PersonJson {
private String name;
private Integer age;
//这里只是随便举个例子,千万不要用中文!!!
@SerializedName(value = “爱好”)
private String hobby;
}
复制代码

示例:

复制代码
public class SecondTest {
JsonObject jsonObject;
JsonArray jsonArray;
@Before
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> index() {
    jsonObject </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> JsonObject();
    jsonObject.addProperty(</span>"name","栗霖"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    jsonObject.addProperty(</span>"age",18<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    jsonArray </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> JsonArray();
    jsonArray.add(</span>"是码农"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    jsonArray.add(</span>"以前喜欢玩游戏"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    jsonArray.add(</span>"现在只敢小心积累"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    jsonArray.add(</span>"只怕突然白了头"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    jsonObject.addProperty(</span>"爱好"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">,jsonArray.toString());
}

@Test
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> formal() {
    Gson gson </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> GsonBuilder().create();
    PersonJson personJson </span>= gson.fromJson(jsonObject.toString(),PersonJson.<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
    System.out.println(</span>"----&gt;String: " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> personJson);
}

}

复制代码

结果:

@serializedName

除了value属性外,还可以使用alternate属性,这个值可以替换前面的值,将传入的json进行修改。

注:value的值不能出现在alternate中,alternate是备选字段。

@SerializedName(value = “hobby”, alternate = {“interest”,“be fond of”})

过滤注解@Expose

注:默认是既可以序列化,也可以反序列化。一定要配合GsonBuilder一起使用

  该注解是加在JavaBean的属性上使用的。

  配合这个使用哦Gson gson = new GsonBuilder().excludeFieldWithoutExposeAnnotation().create();

有四种使用方式:

  1)不使用@Expose注解等同于@Expose(deserialize = false, serialize = false)不做任何解析

  2)@Expose(deserialize = true, serialize = false)只解析使用,可以反序列化,不可以序列化。

  3)@Expose(deserialize = false, serialize = true)可以序列化,不可以反序列化。

  4)@Expose(deserialize = false, serialize = true)既可以序列化,也可以反序列化。

注:这里的序列化指:将对象转化为json字符串。反序列化指:将json字符串转化成对象。

版本控制注解@Since(float v)

结合GsonBuilder().serVersion(n)使用。当n>=v时,才会解析。

注:也是加在JavaBean属性上使用的。

版本控制注解@Util(float v)

与@Since相反,这次是n<v时才能够解析。

使用TypeAdapter来实现序列化与反序列化。

使用TypeAdapter来序列化和反序列化

代码:

复制代码
public class FiveTest {
@Test
public void index() {
Gson gson = new GsonBuilder().create();
TypeAdapter<PersonJson> typeAdapter = gson.getAdapter(PersonJson.class);
String json = “{“name”:“栗霖”,“age”:“18”,“hobby”:“我很是很喜欢FromSoftWare的。大爱宫崎英高,赞美太阳”}”;
PersonJson p = new PersonJson(“栗霖”,18,“混系列忠实粉丝”);
    System.out.println(</span>"----&gt;序列化:是将对象转化为字符串 : " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> typeAdapter.toJson(p));
    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        System.out.println(</span>"----&gt;反序列化:是将字符串转化为对象 : "+<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> typeAdapter.fromJson(json));
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e) {
        e.printStackTrace();
    }
}

}

复制代码

结果:

 

Gson的容错机制

为什么容错:

  如果Bean中定义的是int,但是返回的json对应的是一个""空字串怎么办?这就依赖到了Gson的容错机制。

1)创建宽松Gson

遇到问题,停止解析,以防止报错,功能相对较弱。

Gson gson = new GsonBuilder()
.setLenient()//宽松
.create();

2)自定义TypeAdapter

该方法更倾向于整体防止出现异常。

复制代码
public class ThirdTest {
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">static</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span> PersonTypeAdapter <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">extends</span> TypeAdapter&lt;PersonJson&gt;<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
    @Override
    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span> write(JsonWriter jsonWriter, PersonJson personJson) <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">throws</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> IOException {
        jsonWriter.beginObject();
        jsonWriter.name(</span>"name"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">).value(personJson.getName());
        jsonWriter.name(</span>"age"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">).value(personJson.getAge());
        jsonWriter.name(</span>"hobby"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">).value(personJson.getHobby());
        jsonWriter.endObject();
    }

    @Override
    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> PersonJson read(JsonReader jsonReader) <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">throws</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> IOException {
        PersonJson personJson </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> PersonJson();
        jsonReader.beginObject();
        </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">while</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (jsonReader.hasNext()) {
            </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">switch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (jsonReader.nextName()) {
                </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">case</span> "name"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">:
                    personJson.setName(jsonReader.nextString());
                    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">break</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">;
                </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">case</span> "age"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">:
                    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
                        personJson.setAge(Integer.valueOf(jsonReader.nextString()));
                    } </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e) {
                    }
                    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">break</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">;
                </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">case</span> "hobby"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">:
                    personJson.setHobby(jsonReader.nextString());
            }
        }
        jsonReader.endObject();
        </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> personJson;
    }
}

@Test
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> index() {
    Gson gson </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> Gson();

    String json </span>= "{\"name\":\"栗霖\",\"age\":\"\",\"hobby\":\"篮球吧,读书吧,steam吧\"}"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">;

    System.out.println(</span>"----&gt; " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> json );

    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        PersonJson p1 </span>= gson.fromJson(json,PersonJson.<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
        System.out.println(</span>"----&gt;默认解析 " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> p1);
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e) {
        System.out.println(</span>"----&gt;异常解析,这里json字符串缺失了age的值,真的是怕一转眼就白了头啊" +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">e);
    }

    Gson gson1 </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span> GsonBuilder().registerTypeAdapter(PersonJson.<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span>,<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> PersonTypeAdapter()).create();

    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        PersonJson p2 </span>= gson1.fromJson(json,PersonJson.<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
        System.out.println(</span>"----&gt;自定义Adapter 默认解析 "+<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">p2);
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e) {
        System.out.println(</span>"----&gt;自定义adapter 异常解析" +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> e);
    }

    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        PersonTypeAdapter personTypeAdapter </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> PersonTypeAdapter();
        PersonJson p3 </span>=<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> personTypeAdapter.fromJson(json);
        System.out.println(</span>"----&gt;自定义Adapter 默认解析 " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> p3);
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e){
        System.out.println(</span>"----&gt;自定义Adapter 异常解析 " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">e);
    }
}

}

复制代码

结果:

3)使用注解jsonAdapter,其实质也是自定义Adapter

  该方法更倾向于某一个属性的保护。

实体类:

public class PersonJson {
private String name;
@JsonAdapter(IntegerTypeAdapter.class)
private Integer age;
private String hobby;
}

Adapter:

复制代码
public class IntegerTypeAdapter extends TypeAdapter<Integer>{
@Override
public void write(JsonWriter jsonWriter, Integer integer) throws IOException {
jsonWriter.value(integer);
}
@Override
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> Integer read(JsonReader jsonReader) <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">throws</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> IOException {
    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">int</span> i = 0<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">;
    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        i </span>=<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> Integer.valueOf(jsonReader.nextString());
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e){}
    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> i;
}

}

复制代码

测试类:

复制代码
public class FourTest {
@Test
</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">public</span> <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">void</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> index() {
    Gson gson </span>= <span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> Gson();
    String json </span>= "{\"name\":\"栗霖\",\"age\":\"\",\"hobby\":\"篮球吧,读书吧,steam吧\"}"<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">;

    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        PersonJson p1 </span>= gson.fromJson(json,PersonJson.<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
        System.out.println(</span>"----&gt;默认解析 " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> p1);
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e) {
        System.out.println(</span>"----&gt;异常解析,这里json字符串缺失了age的值,真的是怕一转眼就白了头啊" +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;">e);
    }

    </span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> {
        PersonJson p2 </span>= gson.fromJson(json,PersonJson.<span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">class</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;">);
        System.out.println(</span>"----&gt;默认解析 " +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> p2);
    }</span><span style="font-family:'Courier New';font-size:12px;line-height:1.5;color:rgb(0,0,255);">catch</span><span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> (Exception e) {
        System.out.println(</span>"----&gt;异常解析" +<span style="line-height:1.5;font-family:'Courier New';font-size:12px;"> e);
    }
}

}

复制代码

结果:

 本文参考:http://blog.csdn.net/axuanqq/article/details/51441590,学习整理。如果你觉得喜欢不妨点个赞,如果你觉得哪里有问题欢迎留言讨论。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值