使用json-lib.jar包創建JsonObject

基於json-lib.jar包Json實例程序
1.從頭或者從零開始,創建一個JSONObject(Creating a JSONObject from scratch)
實例1:
   JSONObject jsonObject = new JSONObject();
   jsonObject.element("name", "周星星");
   jsonObject.element("sex", "male");
   jsonObject.element("age", 18);
   jsonObject.element("job", "student");

   System.out.println(jsonObject.get("name"));// 周星星
   System.out.println(jsonObject.get("job"));// student
   System.out.println(jsonObject.getString("sex"));// male
   System.out.println(jsonObject.getInt("age"));// 18
實例2:
   JSONObject jsonObject = new JSONObject().element("string", "JSON").element("integer", "1").element("double", "2.0").element("boolean", "true");
   assertEquals("JSON", jsonObject.getString("string"));
   assertEquals(1, jsonObject.getInt("integer"));
   assertEquals(2.0d, jsonObject.getDouble("double"), 0d);
   assertTrue(jsonObject.getBoolean("boolean"));
   注:這是對實例1的一個簡化版
  
2.使用一個JSON格式化字符串來創建一個JSONObject(Creating a JSONObject from a JSON formatted string)  
實例1:
   String json = "{name:\"周星星\",sex:\"male\",age:18,job:\"student\"}";
   JSONObject jsonObject = JSONObject.fromObject(json);
   //或者使用 JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(json);
   System.out.println(jsonObject.get("name"));// 周星星
   System.out.println(jsonObject.get("job"));// student
   System.out.println(jsonObject.getString("sex"));// male
   System.out.println(jsonObject.getInt("age"));// 18
實例2:
   String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";
   JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str);
   assertEquals("JSON", jsonObject.getString("string"));
   assertEquals(1, jsonObject.getInt("integer"));
   assertEquals(2.0d, jsonObject.getDouble("double"), 0d);
   assertTrue(jsonObject.getBoolean("boolean"));
  
3.使用一個Map來創建一個JSONObject(Creating a JSONObject from a Map)
實例1:
   Map map = new HashMap();  
   map.put( "string", "JSON" );  
   map.put( "integer", "1" );  
   map.put( "double", "2.0" );  
   map.put( "boolean", "true" );  
   JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( map );  
   assertEquals( "JSON", jsonObject.getString("string") );        
   assertEquals( 1, jsonObject.getInt("integer") );        
   assertEquals( 2.0d, jsonObject.getDouble("double"), 0d );        
   assertTrue( jsonObject.getBoolean("boolean") );

4.使用一個JavaBean來創建一個JSONObject(Creating a JSONObject from a JavaBean)
實例1:
public class Mybean {
   private String string;
   private int integer;
   private double dooble;
   private boolean bool;

   // getters & setters
}

Mybean bean = new Mybean();
bean.setString("JSON");
bean.setInteger(1);
bean.setDooble(2.0d);
bean.setBool(true);
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(bean);
assertEquals("JSON", jsonObject.getString("string"));
assertEquals(1, jsonObject.getInt("integer"));
assertEquals(2.0d, jsonObject.getDouble("dooble"), 0d);
assertTrue(jsonObject.getBoolean("bool"));


由此可見,無論要轉換的源是哪種類型,都可以使用(JSONObject) JSONSerializer.toJSON()或JSONObject.fromObject()來轉換

發佈了14 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章