java json的使用

来源:互联网 发布:淘宝客的官方群多少 编辑:程序博客网 时间:2024/05/21 01:55
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
commons系列的包,可在网站:http://www.docjar.com/上面搜索下载,其它包可下载网站如下:

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 );
原创粉丝点击