1. 程式人生 > >json的學習筆記

json的學習筆記

json 學習筆記
json是一種輕量級的資料交換格式。
json格式的資料可以通過線上的
(http:www.jsoneditoronline.org)
來進行轉換
下面是json的例子:

{
"name" : "王小二",
"age" : 25.2,
"birthday" : "1990-01-01",
"school" : "藍翔",
"major" : ["理髮","挖掘機"],
"has_girlfriend" : false,
"car" : null,
"house" : null
}

在eclipse中新建一個maven專案,在pom.xml中引入依賴

<dependencies>
   <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20090211</version>
   </dependency>
</dependencies>

在main方法中

public class JSONDemo {
	public static void main(String[] args) {
		jSONObject();
	}

	private static void jSONObject() {
		JSONObject wangxiaoer=new JSONObject();
		try {
			wangxiaoer.put("name":"王小二");
			wangxiaoer.put("age":25.2);
			wangxiaoer.put("birthday":"1990-01-01");
			wangxiaoer.put("school":"藍翔");
			wangxiaoer.put("major":new String[]{"理髮","挖掘機"});
			wangxiaoer.put("has_girlfriend": false);
			wangxiaoer.put("car":null);
			wangxiaoer.put("house": null);
			System.out.println(wangxiaoer.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}