登录 注册
当前位置:主页 > 资源下载 > 14 > JSON的完整依赖包中包含了处理JSON格式数据所必需的7个包

JSON的完整依赖包中包含了处理JSON格式数据所必需的7个包

  • 更新:2024-12-17 14:17:14
  • 大小:1.59MB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:Java - 后端
  • 格式:RAR

资源介绍

找了很久 的 终于搞好了 JSON数据以来包全部的jar包 解压后放在项目下就可以了 下面付源代码 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Json { public static void main(String[] args) { Json j = new Json(); j.bean2json(); } public void arr2json() { boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); System.out.println(jsonArray); // prints [true,false,true] } public void list2json() { List list = new ArrayList(); list.add("first"); list.add("second"); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); // prints ["first","second"] } public void createJson() { JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']"); System.out.println(jsonArray); // prints ["json","is","easy"] } public void map2json() { Map map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map); System.out.println(json); // prints // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){ // return this.arr[i]; }] } public void bean2json() { JSONObject jsonObject = JSONObject.fromObject(new MyBean()); System.out.println(jsonObject); } public void json2bean() { String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}"; JSONObject jb = JSONObject.fromString(json); JSONObject.toBean(jb, MyBean.class); System.out.println(); } } 操作的bean: import net.sf.json.JSONFunction; public class MyBean { private String name = "json"; private int pojoId = 1; // private char[] options = new char[] { 'a', 'f' }; private String func1 = "function(i){ return this.options[i]; }"; private JSONFunction func2 = new JSONFunction(new String[] { "i" }, "return this.options[i];"); // getters & setters ...... }