1、把JSONS字符串转换成指定的java对象
User user = new User();
user=JSON.parseObject(jsonData, User.class);
2、把java对象转换成JSON数据
User user = new User();
String jsonData = JSON.toJSONString(object);
3、把JSON数据转换成指定java对象的List
List<User> list = new ArrayList<User>();
list = JSONObject.parseArray(jasonArray, User.class);
抽象类JSON中的常用方法(部分):
public static Object parse(String text);//将字符串解析为对象
public static JSONArray parseArray(String text);//将字符串解析为JSONArray
public static <T> List<T> parseArray(String text, Class<T> clazz);//将字符串解析为Listpublic static List<Object> parseArray(String text, Type[] types);//将字符串解析为List
public static <T> T parseObject(String text, Class<T> clazz);//将字符串解析为对象
public static JSONObject parseObject(String text)//将字符串解析为JSONObeject
public static Object toJSON(Object javaObject)//
public static String toJSONString(Object object)//将对象转换为JSONString
类JSONObject中的常用方法(部分):
public boolean containsKey(Object key)//是否包含指定对象Key,返回布尔值
public boolean containsValue(Object value)//是否包含指定对象Value,返回布尔值
public Object get(Object key)//通过对象Key,返回对象
public Boolean getBoolean(String key)//通过字符串Key,返回布尔值
public int getIntValue(String key)//通过指定字符串Key,返回int
public JSONArray getJSONArray(String key)//通过指定字符串Key,返回JSONArray
public JSONObject getJSONObject(String key)//通过指定字符串Key,返回JSONObject
public <T> T getObject(String key, Class<T> clazz)//通过指定字符串Key和类对象,返回指定的对象
类JSONArray中的常用方法(部分):
public boolean add(Object e)//添加指定对象,添加成功返回true,否则返回false
public boolean contains(Object o)//是否包含指定对象,包含返回true,不包含返回false
public Object get(int index)//通过索引返回对象
public Boolean getBoolean(int index)//通过索引返回布尔值
public Long getLong(int index)//通过索引返回Long
public JSONObject getJSONObject(int index)//通过索引返回JSONObject
public Object remove(int index)//通过索引删除指定对象,并返回删除的对象
public Object set(int index, Object element)//设置指定索引的元素,返回原有对象
public int size() //返回JSONArray的大小