JSON簡介及JSONObject生成json

1、JSON簡介html

JSON:JavaScript對象表示法(JavaScript Object Notation)java

相似xml  :存儲和交換信息的語法json

比xml更快、更易解析數組

JSON語法規則:spa

  一、數據在名稱/值對中.net

  二、數據有逗號分隔code

  三、花括號保存對象xml

  四、方括號保存數組htm

JSON值:對象

  一、數字:整數或浮點數

  二、字符串:在雙引號中

  三、邏輯值:true false

  四、數組:在方括號中

  五、對象:在花括號中

  六、null

2、在JAVA中使用JSONObject生成json

所須要的包

commons-httpclient-3.1.jar

commons-lang-2.4.jar

commons-logging-1.1.1.jar

json-lib-2.4-jdk15.jar

ezmorph-1.0.6.jar

commons-collections-3.2.1.jar

 一、bean轉爲Json

 
User u = new User();
u.setAge(22);
u.setUsername("hzucmj");
u.setEnabled(true);

JSONObject json = JSONObject.fromObject(u);
System.out.println(json.toString()); //結果爲:{"enabled":true,"username":"hzucmj","age":22}

二、List轉爲Json

 
User u1 = new User();
u1.setAge(22);
u1.setUsername("hzucmj");
u1.setEnabled(true);

User u2 = new User();
u2.setAge(20);
u2.setUsername("ctf");
u2.setEnabled(true);

List<Object> list = new ArrayList<Object>();
list.add(u1);</p>
list.add(u2);</p>

JSONArray json = JSONArray.fromObject(list);
System.out.println(json.toString()); //結果爲:[{"enabled":false,"username":"ctf","age":20},{"enabled":false,"username":"","age":0}]

三、Map轉爲Json

HashMap<String, Comparable> map = new HashMap<String, Comparable>();
map.put("name", "hzucmj");
map.put("age", 22);
JSONObject json = JSONObject.fromObject(map);
System.out.println(json.toString()); //結果爲:{"name":"hzucmj","age":22}


3、JSON轉換的四種各類狀況:

1. //把java 對象列表轉換爲json對象數組,並轉爲字符串

    JSONArray array = JSONArray.fromObject(userlist);
    String jsonstr = array.toString();

2.//把java對象轉換成json對象,並轉化爲字符串

  JSONObject object = JSONObject.fromObject(invite);
   String str=object.toString());

3.//把JSON字符串轉換爲JAVA 對象數組

  String personstr = getRequest().getParameter("persons");
  JSONArray json = JSONArray.fromObject(personstr);
  List<InvoidPerson> persons = (List<InvoidPerson>)JSONArray.toCollection(json, nvoidPerson.class);
4.//把JSON字符串轉換爲JAVA 對象

  JSONObject jsonobject = JSONObject.fromObject(str);
  PassportLendsEntity passportlends = null;
  try {
   //獲取一個json數組
   JSONArray array = jsonobject.getJSONArray("passports");
   //將json數組 轉換成 List<PassPortForLendsEntity>泛型
   List<PassPortForLendsEntity> list = new ArrayList<PassPortForLendsEntity>();
   for (int i = 0; i < array.size(); i++) {   
            JSONObject object = (JSONObject)array.get(i);  
            PassPortForLendsEntity passport = (PassPortForLendsEntity)JSONObject.toBean(object,
              PassPortForLendsEntity.class);
            if(passport != null){
             list.add(passport);
            }  
     }
   //轉換PassportLendsEntity 實體類
  passportlends = (PassportLendsEntity)JSONObject.toBean(jsonobject, PassportLendsEntity.class);

  str = "{\"lendperson\":\"李四\",\"lendcompany\":\"有限公司\",\"checkperson\":\"李四\",

  \"lenddate\":\"2010-07-19T00:00:00\",\"lendcounts\":4,\"
  passports\":[{\"passportid\":\"d\",\"name\":\"李豫川\",\"passporttype\":\"K\"},
  {\"passportid\":\"K9051\",\"name\":\"李平\",\"passporttype\":\"K\"},
  {\"passportid\":\"K90517\",\"name\":\"袁寒梅\",\"passporttype\":\"K\"},
  {\"passportid\":\"K905199\",\"name\":\"賀明\",\"passporttype\":\"K\"}]}";


 本文部分轉載至:http://blog.csdn.net/ygzk123/article/details/8273293

http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html

相關文章
相關標籤/搜索