FastJson下载:
https://github.com/alibaba/fastjson
http://repo1.maven.org/maven2/com/alibaba/fastjson/1.1.51.android/
Gson下载:
https://mvnrepository.com/artifact/com.google.code.gson/gson
总结:
Gson,FastJson,手动解析。效率最高的就是阿里巴巴公司开发的FastJson!
1.Ctrl+Alt+A:截图
2.Gson:
(1)将json格式的字符串{}转换为Java对象
ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
(2)将json格式的字符串[]转换为Java对象的List
List<ShopInfo> shops = gson.fromJson(json, new TypeToken<List<ShopInfo>>() {}.getType());
(3)将Java对象转换为json字符串{}
String json = gson.toJson(shopInfo);
(4)将Java对象的List转换为json字符串[]
String json = gson.toJson(shops);
3.FastJson( 推荐):
(1)将json格式的字符串{}转换为Java对象
ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
(2)将json格式的字符串[]转换为Java对象的List
List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
(3)将Java对象转换为json字符串{}
String json = JSON.toJSONString(shopInfo);
(4)将Java对象的List转换为json字符串[]
String json = JSON.toJSONString(shops);
4.FastJson工具:(转载自:https://blog.csdn.net/u010310183/article/details/51396290)
/** * fastjson工具类 * @version:1.0.0 */ public class FastJsonUtils { private static final SerializeConfig config; static { config = new SerializeConfig(); config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 } private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段 SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null }; public static String toJSONString(Object object) { return JSON.toJSONString(object, config, features); } public static String toJSONNoFeatures(Object object) { return JSON.toJSONString(object, config); } public static Object toBean(String text) { return JSON.parse(text); } public static <T> T toBean(String text, Class<T> clazz) { return JSON.parseObject(text, clazz); } // 转换为数组 public static <T> Object[] toArray(String text) { return toArray(text, null); } // 转换为数组 public static <T> Object[] toArray(String text, Class<T> clazz) { return JSON.parseArray(text, clazz).toArray(); } // 转换为List public static <T> List<T> toList(String text, Class<T> clazz) { return JSON.parseArray(text, clazz); } /** * 将javabean转化为序列化的json字符串 * @param keyvalue * @return */ public static Object beanToJson(KeyValue keyvalue) { String textJson = JSON.toJSONString(keyvalue); Object objectJson = JSON.parse(textJson); return objectJson; } /** * 将string转化为序列化的json字符串 * @param keyvalue * @return */ public static Object textToJson(String text) { Object objectJson = JSON.parse(text); return objectJson; } /** * json字符串转化为map * @param s * @return */ public static Map stringToCollect(String s) { Map m = JSONObject.parseObject(s); return m; } /** * 将map转化为string * @param m * @return */ public static String collectToString(Map m) { String s = JSONObject.toJSONString(m); return s; } }
5.Gson工具:
public class GsonUtil { private static Gson gson = null; static { if (gson == null) { gson = new Gson(); } } private GsonUtil() { } /** * 转成json * * @param object * @return */ public static String GsonString(Object object) { String gsonString = null; if (gson != null) { gsonString = gson.toJson(object); } return gsonString; } /** * 转成bean * * @param gsonString * @param cls * @return */ public static <T> T GsonToBean(String gsonString, Class<T> cls) { T t = null; if (gson != null) { t = gson.fromJson(gsonString, cls); } return t; } /** * 转成list * 泛型在编译期类型被擦除导致报错 * * @param gsonString * @param cls * @return */ public static <T> List<T> GsonToList(String gsonString, Class<T> cls) { List<T> list = null; if (gson != null) { list = gson.fromJson(gsonString, new TypeToken<List<T>>() { }.getType()); } return list; } /** * 转成list * 解决泛型问题 * * @param json * @param cls * @param <T> * @return */ public static <T> List<T> jsonToList(String json, Class<T> cls) { Gson gson = new Gson(); List<T> list = new ArrayList<T>(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); for (final JsonElement elem : array) { list.add(gson.fromJson(elem, cls)); } return list; } /** * 转成list中有map的 * * @param gsonString * @return */ public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) { List<Map<String, T>> list = null; if (gson != null) { list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() { }.getType()); } return list; } /** * 转成map的 * * @param gsonString * @return */ public static <T> Map<String, T> GsonToMaps(String gsonString) { Map<String, T> map = null; if (gson != null) { map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() { }.getType()); } return map; } }