Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转

前端之家收集整理的这篇文章主要介绍了Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_2@String、JsonObject、JavaBean 互相转换
    User user = new Gson()@H_404_2@.fromJson(jsonObject,User@H_404_2@.class);
    User user = new Gson()@H_404_2@.fromJson(@H_404_2@string,User@H_404_2@.class);
    @H_404_2@String @H_404_2@string = new Gson()@H_404_2@.toJson(user);
    JsonObject jsonObject = new Gson()@H_404_2@.toJsonTree(user)@H_404_2@.getAsJsonObject(); 
    JsonObject jsonObject = new JsonParser()@H_404_2@.parse(@H_404_2@string)@H_404_2@.getAsJsonObject();
@H_404_2@String、JsonArray、@H_404_2@List互相转换
    @H_404_2@List<User> userList = gson@H_404_2@.fromJson(@H_404_2@string,new TypeToken<@H_404_2@List<User>>() {}@H_404_2@.getType()); 
    @H_404_2@List<User> userList = gson@H_404_2@.fromJson(jsonArray,new TypeToken<@H_404_2@List<User>>() {}@H_404_2@.getType()); 
    @H_404_2@String @H_404_2@string = new Gson()@H_404_2@.toJson(userList); 
    JsonArray jsonArray = new Gson()@H_404_2@.toJsonTree(userList,new TypeToken<@H_404_2@List<User>>() {}@H_404_2@.getType())@H_404_2@.getAsJsonArray();
    JsonArray jsonArray = new JsonParser()@H_404_2@.parse(@H_404_2@string)@H_404_2@.getAsJsonArray();

猜你在找的Json相关文章