一 Object和String互转
1.Object转换String
String jsonStr = JSONObject.toJSONString(object);
String jsonStr = JSONObject.toJSONString(object);
String jsonStr = (String) JSON.toJSONString(object);
String jsonStr = new Gson().toJson(object);
2.String转换Object
Object o = JSONObject.parseObject(jsonStr);
二 Object,JSONObject 转对象(Bean)
1.Object转换Bean
Bean bean =object;
2.JSONObject转换Bean
Bean bean = JSONObject.toJavaObject(jsonObject, Bean.class);
Bean data = JSONObject.parseObject(String.valueOf(jsonObject), Bean.class);
三 JSONObject 和String互转
1.String转JSONObject
JSONObject jsonObject = JSONObject.parseObject(str);
2.JSONObject转String
String jsonStr = JSONObject.toJSONString(jsonObject);
String jsonStr = (String) JSON.toJSONString(jsonObject);
String jsonStr = new Gson().toJson(jsonObject);
四 获取JSONObject的具体内容
JSONObject menuObj = JSONObject.parseObject(str);
// 获取ArrayList
JSONArray js = menuObj.getJSONArray(DataParser.Parent.menus);
// 获取字符串
String str = menuObj.getString(DataParser.Parent.menuStyle);
public class DataParser {
public static class Parent {
public static String menuStyle = "menuStyle";
public static String menus = "menus";//菜单数组
}
public static class Child {
public static String imageUrl = "imageUrl";
}
}