public static void main(String[] args) {
MallGoodsHome home = new MallGoodsHome();
home.setGoodstotal(100l);
home.setGoodsname("行车记录仪");
//实体类转jsonObject toJSON(Entity)
JSONObject j1 = (JSONObject)JSONObject.toJSON(home);
System.out.println("实体类=>> jsonObject:"+j1);
//对象转json字符串 toJSONString(home)
String jsonString = JSONObject.toJSONString(home);
System.out.println("对象=>> json字符串:"+jsonString);
//json字符串转jsonObject parseObject(jsonString)
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println("json字符串=>> jsonObject:"+jsonObject);
//jsonObject转 json字符串 toJSONString()
String s = jsonObject.toJSONString();
System.out.println("jsonObject=>> json字符串:"+s);
//json字符串 转 对象 JSONObject.parseObject(jsonString, Entity.class);
MallGoodsHome home1 = JSONObject.parseObject(jsonString, MallGoodsHome.class);
System.out.println("json字符串=>> 对象:"+home1.toString());
MallGoodsHome home2 = new MallGoodsHome();
home2.setGoodstotal(200l);
home2.setGoodsname("触屏电脑");
ArrayList<MallGoodsHome> list = new ArrayList<>();
list.add(home);
list.add(home2);
//list 转 json数组 :先转为字符串 然后字符串转jsonArray
String s1 = JSONObject.toJSONString(list);
//字符串转jsonArray parseArray
JSONArray objects = JSONArray.parseArray(s1);
System.out.println("数组=>> json数组"+objects);
//json数组字符串 转 list 先转为字符串 然后字符串转List
List<MallGoodsHome> list1 = JSONObject.parseArray(objects.toJSONString(), MallGoodsHome.class);
System.out.println("json数组字符串=>> list"+list1);
}
fastjson 使用小记
于 2022-01-17 13:56:10 首次发布