package util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class JSONTest {
private String name;
private String password;
public JSONTest() {
super();
}
public JSONTest(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static void main(String[] args) {
//JSONArray用法
//獲得JSONArray對象
//第一種創建JSONArray對象
JSONArray jarray = new JSONArray();
//第二種通過方法獲取,JSONArray裏面有許多方法返回的是JSONArray對象
//如:fromObject方法等等
/***
* JSONObject和JSONArray的區別
* 區別在於JSONObject是一個{}包裹起來的一個對象(Object),
而JSONArray則是[]包裹起來的一個數組(Array),
說白點就是一個是數組一個是對象或字符串
JSONObjec 有鍵名,JSONArray沒有。
所以解析的時候JSONObject是JSONObject.getString("msg"),
而JSONArray是JSONArray.getString(5),這個數字5是位置
第二個要注意的是,JSONObject不能轉換List類型,
JSONObject.fromObject(list) 會報錯
* ***/
/***
*
* 準確說,是如果裏面是鍵值對的話,比如 {"3":"3","null":"0","2":"2","1":"1","5":"5","4":"4"}
* 外面一定要使用{}大括號,如果裏面是["1","2"]
* 這類型的,一定要使用[]中括號
* */
/**
* List集合轉換爲json代碼
* */
List list = new ArrayList();
list.add("1");
list.add("2");
JSONArray jlist = JSONArray.fromObject(list);
//打印查看
System.out.println(jlist);
//結果 ["1","2"]
//獲得索引相關的字符串
System.out.println(jlist.getString(1));
//結果 2
/**
* List集合內含對象轉換爲json代碼,
* **/
JSONTest j1 = new JSONTest("admin","123456");
JSONTest j2 = new JSONTest("yonghu","123");
List<JSONTest> claList = new ArrayList<JSONTest>();
claList.add(j1);
claList.add(j2);
JSONArray jiequ = JSONArray.fromObject(claList);
System.out.println(jiequ);
//結果 [{"name":"admin","password":"123456"},{"name":"yonghu","password":"123"}]
//獲得list的第二個值的json格式
System.out.println(jiequ.getString(1));
//結果 {"name":"yonghu","password":"123"}
JSONObject root = jiequ.getJSONObject(1);
System.out.println(root);
//結果 {"name":"yonghu","password":"123"}
System.out.println(root.get("name"));
//結果 yonghu
System.out.println(root.get("password"));
//結果 123
/**
* 將實體類 轉換爲 JSONObject
* **/
System.out.println("--------------------Object----------------------");
JSONObject job = JSONObject.fromObject(new JSONTest("yc","123"));
System.out.println(job);
//獲得name屬性值
System.out.println(job.get("name"));
//結果 yc
//獲得password屬性值
System.out.println(job.get("password"));
//結果 123
/**
* JSONObject轉換爲實體類
* **/
JSONTest jt = (JSONTest) JSONObject.toBean(job,JSONTest.class);
System.out.println(jt.getName()+":"+jt.getPassword());
System.out.println("--------------------Map----------------------");
/**
* Map集合轉換爲json代碼
* HashMap和Hashtable 區別
* 第一個不同主要是歷史原因。Hashtable是基於陳舊的Dictionary類的,HashMap是Java 1.2引進的Map接口的一個實現。
* 最重要的不同是Hashtable的方法是同步的,而HashMap的方法不是。這就意味着,雖然你可以不用採取任何特殊的行爲就可以在一個多線程的應用程序中用一個Hashtable,
* 但你必須同樣地爲一個HashMap提供外同步。
* 第三點 ,HashMap可以允許有空的鍵和值,Hashtable則不可以
* */
Map map = new HashMap();
map.put(null, "0");
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
map.put("5", "5");
JSONArray jmap = JSONArray.fromObject(map);
System.out.println(jmap);
//結果 [{"3":"3","null":"0","2":"2","1":"1","5":"5","4":"4"}]
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
//結果 {"3":"3","null":"0","2":"2","1":"1","5":"5","4":"4"}
//後臺遍歷json的map的值
Iterator it = json.entrySet().iterator();
while(it.hasNext()) {
Entry entry = (Entry)it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
/**
* 將json對象轉換爲List類型 ,對於list中包含爲類的
* */
//List<JSONTest> lj = JSONArray.toList(jiequ, JSONTest.class);
//等價上面的代碼
List<JSONTest> lj = JSONArray.toList(jiequ, new JSONTest(),new JsonConfig());
for(JSONTest j : lj) {
System.out.println(j.getName()+":"+j.getPassword());
}
/**
* 將json對象轉換爲List類型 ,對於list中包含字符串等基本類型
* **/
List<String> lt = JSONArray.toList(jlist);
for(String s: lt) {
System.out.println(s);
}
}
}
json的基本用法(list.map,pojo等和json互轉)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.