基于json-lib.jar包Json实例程序

基于json-lib.jar包Json实例程序

 
创建JSONArray的常用四种方法
 
1.从头或者从零开始,创建一个JSONArray(Creating a JSONArray from scratch)
实例1: 
JSONArray jsonArray = new JSONArray().element("JSON").element("1").element("2.0").element("true"); 
assertEquals("JSON", jsonArray.getString(0)); 
assertEquals(1, jsonArray.getInt(1)); 
assertEquals(2.0d, jsonArray.getDouble(2), 0d); 
assertTrue(jsonArray.getBoolean(3)); 
   
3.使用一个Collection来创建一个JSONArray(Creating a JSONArray from a Collection)
实例1:
  
List list = new ArrayList();
 list.add( "JSON" ); 
list.add( "1" ); 
list.add( "2.0" ); 
list.add( "true" ); 
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( list ); assertEquals( "JSON", jsonArray.getString(0) ); 
assertEquals( 1, jsonArray.getInt(1) ); 
assertEquals( 2.0d, jsonArray.getDouble(2), 0d ); 
assertTrue( jsonArray.getBoolean(3) ); 
 
   
4.使用一个Array来创建一个JSONArray(Creating a JSONArray from an array)
实例1:
Object[] array = new Object[] { "JSON", 1, 2.0, true }; 
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(array); assertEquals("JSON", jsonArray.getString(0));
assertEquals(1, jsonArray.getInt(1)); 
assertEquals(2.0d, jsonArray.getDouble(2), 0d); 
assertTrue(jsonArray.getBoolean(3)); 

//实例2: 
Object[] array = new Object[] { "JSON", 1, 2.0, true }; 
JSONArray jsonArray = JSONArray.fromObject(array); 
assertEquals("JSON", jsonArray.getString(0)); 
assertEquals(1, jsonArray.getInt(1)); 
assertEquals(2.0d, jsonArray.getDouble(2), 0d); 
assertTrue(jsonArray.getBoolean(3)); 
    
由此可见,无论要转换的源是哪种类型,都可以使用(JSONArray) JSONSerializer.toJSON()或JSONArray.fromObject()来转换;
 
   常用的三种扩展功能 
 
1.使用一个JSON格式化字符串来创建一个JSONFunction(JavaScript functions)  
实例:
String str = "{func: function( param ){ doSomethingWithParam(param); }}"; 
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str); JSONFunction func = (JSONFunction) jsonObject.get("func"); ArrayAssertions.assertEquals(new String[] { "param" }, func.getParams()); 
assertEquals("doSomethingWithParam(param);", func.getText()); 
  
 
注:函数的写法要注意function(){},参数param是可选的!

2.排除一个JSON格式化字符串的某些属性(Exclude properties)
实例:
   
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; 
JsonConfig jsonConfig = new JsonConfig(); 
jsonConfig.setExcludes(new String[] { "double", "boolean" }); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str, jsonConfig); 
assertEquals("JSON", jsonObject.getString("string")); 
assertEquals(1, jsonObject.getInt("integer"));
assertFalse(jsonObject.has("double")); 
assertFalse(jsonObject.has("boolean")); 
    
3.排除一个JSON格式化字符串的某些属性,使用过滤器(Exclude properties (with filters)
实例:
  
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";
 JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { 
   public boolean apply(Object source, String name, Object value) {
         if ("double".equals(name) || "boolean".equals(name)) 
          { return true; }
              return false; } }); 
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str, jsonConfig); 
assertEquals("JSON", jsonObject.getString("string")); 
assertEquals(1, jsonObject.getInt("integer")); 
assertFalse(jsonObject.has("double")); 
assertFalse(jsonObject.has("boolean")); 
 
 

JSONObject-Java常用的四种用法
1.JSONObject to DynaBean

String json = "{name=\"json\",bool:true,int:1,double:2.2}"; JSONObject jsonObject = JSONObject.fromObject(json); //抽象的写法:DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject ); Object bean = JSONObject.toBean(jsonObject); 
//Object bean1 = JSONSerializer.toJava(jsonObject); 
assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean, "name"));
 assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool")); 
assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int")); 
assertEquals(jsonObject.get("double"), PropertyUtils.getProperty(bean, "double")); 

 

 

 2.JSONObject to JavaBean

String json = "{name:\"zhangsan\",age:25,hight:1.72,sex:true}"; JSONObject jsonObject = JSONObject.fromObject(json); 
UserBean bean = (UserBean) JSONObject.toBean(jsonObject, UserBean.class); 
System.out.println(jsonObject); 

  

 


3.JSONArray to List

String json = "[\"first\",\"second\"]"; 
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
 List output = (List) JSONSerializer.toJava(jsonArray); 

    
4.JSONArray to array

 

String json = "[\"first\",\"second\"]";
 JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); JsonConfig jsonConfig = new JsonConfig(); 
jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY); 
Object[] output = (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig); 
Object[] expected = new Object[] { "first", "second" }; ArrayAssertions.assertEquals(expected, output);  
 

 

理论上,这样就可以了,但时,有异常Caused by: java.lang.NoSuchMethodException: com.json.Json$UserBean.<init>()

   
2.使用一个JSON格式化字符串来创建一个JSONArray(Creating a JSONArray from a JSON formatted string)  
实例1:
  
String str = "['JSON', 1, 2.0, true]"; //注意该字符串的格式"[ , ]" JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( str ); assertEquals( "JSON", jsonArray.getString(0) ); 
assertEquals( 1, jsonArray.getInt(1) ); 
assertEquals( 2.0d, jsonArray.getDouble(2), 0d ); 
assertTrue( jsonArray.getBoolean(3) ); 
    

 List<String> list = new ArryList<String>(); list.add("a"); list.add("b"); Map<String,List<String>> map = new HashMap<String,List<String>>(); map.put("list",list); MessageUtils.outputJSONResult(JSONObject.fromObject(map).toString(), response); public class MessageUtils { public static void outputJSONResult(String result, HttpServletResponse response) { try { response.setHeader("ContentType", "text/json"); response.setCharacterEncoding("utf-8"); PrintWriter pw = response.getWriter(); pw.write(result); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } }

你可能感兴趣的:(JavaScript,json,bean)