Java代码
/** * Bean.java 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];"); */ JSONObject jsonObject = JSONObject.fromObject( new JsonBean() ); System.out.println( jsonObject ); //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }} /** * Bean.java 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];"); */ JSONObject jsonObject = JSONObject.fromObject( new JsonBean() ); System.out.println( jsonObject ); //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}4、BEANS
Java代码
/** * private int row ; private int col ; private String value ; * */ List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list); System.out.println( ja.toString() ); //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}] /** * private int row ; private int col ; private String value ; * */ List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list); System.out.println( ja.toString() ); //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]5、String to bean