net.sf.json.JSONObject;
net.sf.json.JsonConfig;
使用这个可以方便的将java字符串和java对象与json字符串和对象实现互转。
需要导入的包如下:
commons-beanutils.jar
commons-collections.jar
commons-lang.jar
commons-logging-1.1.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
用法:
1.
List list = new ArrayList<>();
User user = new User(12L, "JSON", "json");
list.add(user);
JsonConfig jsonConfig = new JsonConfig();
JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
System.out.println(jsonArray);
2.
User user = new User(12L, "JSON", "json");
JSONObject json = new JSONObject().fromObject(user);
System.out.println(json);
3.
List list = new ArrayList<>();
User user = new User(12L, "JSON", "json");
list.add(user);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(
jsonArray
);