java——json工具类(json字符串转实体bean)、bean和字符串之间的转换、打印bean

本文使用谷歌的Gson来解析,导入包:
implementation 'com.google.code.gson:gson:2.2.+'

首先在Android studio上安装插件 GsonFormat-Plus,安装好了之后新建一个bean类,然后光标停在类名中按下alt+s(注意:需要新建java文件才能使用,kotlin文件中没反应):

 然后把想要转换的json字符串放进去,然后点设置:

 一直确认就可以生成bean的实体了。

jsonUtil的工具类:

package com.example.test2;import com.google.gson.Gson;
import com.google.gson.JsonNull;
import com.google.gson.JsonSyntaxException;
import org.json.JSONObject;
import java.lang.reflect.Type;public class JsonUtil {private JsonUtil() {}//饿汉单例private static final JsonUtil instance = new JsonUtil();public static JsonUtil getInstance(){return instance;}private static Gson gson = new Gson();/*** @param src :将要被转化的对象* @return :转化后的JSON串* @MethodName : toJson* @Description : 将对象转为JSON串,此方法能够满足大部分需求*/public static String toJson(Object src) {if (null == src) {return gson.toJson(JsonNull.INSTANCE);}try {return gson.toJson(src);} catch (JsonSyntaxException e) {e.printStackTrace();}return null;}/*** @param json* @param classOfT* @return* @MethodName : fromJson* @Description : 用来将JSON串转为对象,但此方法不可用来转带泛型的集合*/public static  Object fromJson(String json, Class classOfT) {try {return gson.fromJson(json, (Type) classOfT);} catch (JsonSyntaxException e) {System.out.println(e.toString() + "------------------------------");e.printStackTrace();}return null;}/*** @param json* @param typeOfT* @return* @MethodName : fromJson* @Description : 用来将JSON串转为对象,此方法可用来转带泛型的集合,如:Type为 new* TypeToken>(){}.getType()* ,其它类也可以用此方法调用,就是将List替换为你想要转成的类*/public static Object fromJson(String json, Type typeOfT) {try {return gson.fromJson(json, typeOfT);} catch (JsonSyntaxException e) {e.printStackTrace();}return null;}/*** 获取json中的某个值** @param json* @param key* @return*/public static String getValue(String json, String key) {try {JSONObject object = new JSONObject(json);return object.getString(key);} catch (Exception e) {e.printStackTrace();}return null;}/*** 获取json中的list值** @param json* @return*/public static String getListValue(String json) {try {JSONObject object = new JSONObject(json);return object.getString("list");} catch (Exception e) {e.printStackTrace();}return null;}public static Double getDoubleValue(String json, String key) {try {JSONObject object = new JSONObject(json);return object.getDouble(key);} catch (Exception e) {e.printStackTrace();}return null;}public static int getIntValue(String json, String key) {try {JSONObject object = new JSONObject(json);return object.getInt(key);} catch (Exception e) {e.printStackTrace();}return 0;}
}

使用:

 String str="{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"links\":[{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"}]}";TestBean oo= (TestBean) JsonUtil.getInstance().fromJson(str,TestBean.class);Log.e("wangyao",oo.getName());Log.e("wangyao",oo.getLinks().get(1).getUrl());

kotlin bean和字符串之间的转换

    /*** json字符串 转 bean*/open fun  strToBean(json: String?, classOfT: Class?): T? {try {val gson = Gson()return gson.fromJson(json, classOfT)} catch (e: JsonSyntaxException) {println("$e------------------------------")e.printStackTrace()}return null}/*** bean 转 json字符串*/fun beanToStr(src: Any?): String? {if (null == src) {return Gson().toJson(JsonNull.INSTANCE)}try {return Gson().toJson(src)} catch (e: JsonSyntaxException) {e.printStackTrace()}return null}

使用:

val user = User("张三", "18")val str = beanToStr(user)Log.e("TAG", "===1" + str)val aaBean = strToBean(str, User::class.java)Log.e("TAG", "===2" + aaBean?.studentName)data class User(val studentName: String, val studentAge: String)

输出:

打印bean

调用beanToStr方法即可打印,查看


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部