1,JSON操作json
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; //json操作数据
public static String objToJson(User user)
{
JSONObject jsonObject = JSONObject.fromObject(user);
return jsonObject.toString();
}
public static User jsonToObj(String str)
{
JSONObject jsonObject = JSONObject.fromObject(str);
User user = (User)jsonObject.toBean(jsonObject, User.class);
return user;
}
public static JSONArray strToJsonArray(List<User> users)
{
return JSONArray.fromObject(users);
}
2,GSON操作json
import com.google.gson.Gson; //gson操作数据
public static String objToJson(User user)
{
Gson gson = new Gson();
return gson.toJson(user);
}
public static User jsonToObj(String str)
{
Gson gson = new Gson();
User user5 = (User)gson.fromJson(str, User.class);
return user5;
}