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();
}
}
}