alibaba.fastjson用法

用于object、json、list、string之间的相互转换

示例:

Listlist = new ArrayList<>();

ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setNumber(1);
shoppingCart.setPrice("5999");
shoppingCart.setTiltle("商品标题");
shoppingCart.setImage(2131165420);

list.add(shoppingCart);

//对象打印结果:
//ShoppingCart{"number":1,"price":"5999","title":"商品标题","image":2131165420}
//list打印结果:
//[{"number":1,"price":"5999","title":"商品标题","image":2131165420}]

//实体类转json
JSONObject jsonObject = (JSONObject)JSONObject.toJSON(shoppingCart);

//json转实体类
ShoppingCart shoppingCart = JSONObject.parseObject(jsonObject.toJSONString(), ShoppingCart.class);
String str = "{"number":1,"price":"5999","title":"商品标题","image":2131165420}";

//String转JSONObject
JSONObject jsonObject2 = JSONObject.parseObject(str);

//JSONObject转String
String str2 = jsonObject2.toJSONString();


//String转json数组
String str3 = "[{"number":1,"price":"5999","title":"商品标题","image":2131165420}]";
JSONArray jsonArray = JSONObject.parseArray(str3);

//json数组转String
String str4 = jsonArray.toJSONString();

//list转json数组
JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONString(list));

//json数组转list
List list2 = JSONObject.parseArray(jsonArray.toJSONString(), ShoppingCart.class);

你可能感兴趣的:(java,java)