首先導入json.jar,非常簡單看過代碼就明白怎么用了
package cn.mylucene;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MyJson {
public static void main(String[] args) throws JSONException {
ParseJson("{\"name\":\"Alexia\",\"age\":\"23\"}");
CreateJson();
}
//解析JSON
private static void ParseJson(String jsonString) throws JSONException
{
JSONObject jObject = new JSONObject(jsonString);
System.out.println(jObject.get("name"));
}
//創建一個JSON
private static void CreateJson() throws JSONException
{
// 下面構造兩個map、一個list和一個Employee對象
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "Alexia");
map1.put("sex", "female");
map1.put("age", "23");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("name", "Edward");
map2.put("sex", "male");
map2.put("age", "24");
// 將Map轉換為JSONArray數據
JSONArray jArray = new JSONArray();
jArray.put(map1);
jArray.put(map2);
//定義JSON
JSONObject jObject=new JSONObject();
jObject.put("List", jArray);
jObject.put("Count","1200");
System.out.println(jObject.toString());
}
}
