java中json資料的生成以及解析
阿新 • • 發佈:2019-02-19
一.json理解
1.json(JavaScript Object Notation):javascript物件表示法(採用javascript物件的語法來表示物件資料),它是一種儲存和傳輸資料的格式,它的優點是易讀,易解析,相比起傳統的xml來說,它具有更小,更快,易解析的優點。—更詳細請參看W3C
二.利用java生成json資料的方式
1.使用org.json jar包中的json API中的JSONObject類
jar包的maven依賴如下:
<dependency>
<groupId>org.json</groupId >
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
java程式碼如下:
Object nullObject = null;
JSONObject wangxiaoer = new JSONObject();
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("house", nullObject);
wangxiaoer.put("car", nullObject);
wangxiaoer.put ("comment", "這是一個註釋");
System.out.println(wangxiaoer.toString());
eclipse中的執行效果截圖如下:
經過json線上編輯器格式化以後效果如下所示:
2.使用hashmap來生成json資料,這種方式會使用JSONObject類的一個構造方法new JSONObject(),此構造方法的入參為Map型別的介面
java程式碼如下:
Map<String,Object> wangxiaoer=new HashMap<String,Object>();
Object nullObject = null;
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("house", nullObject);
wangxiaoer.put("car", nullObject);
wangxiaoer.put("comment", "這是一個註釋");
System.out.println(new JSONObject(wangxiaoer).toString());
最終eclipse中的執行效果同上。
3.使用javabean來生成json資料,這種方式也會使用JSONObject類的一個構造方法new JSONObject(),此構造方法的入參為javabean型別的例項
程式碼如下:
DiaoSi wangxiaoer=new DiaoSi();
wangxiaoer.setName("王小二");
wangxiaoer.setAge(25.2);
wangxiaoer.setBirthday("1990-01-01");
wangxiaoer.setSchool("藍翔");
wangxiaoer.setMajor(new String[] { "理髮", "挖掘機" });
wangxiaoer.setHas_girlfriend(false);
wangxiaoer.setCar(null);
wangxiaoer.setHouse(null);
wangxiaoer.setComment("這是一個註釋");
System.out.println(new JSONObject(wangxiaoer).toString());
最終的執行效果同上
三.json資料的解析
此時會用到apache專案組下的commons-io jar包,maven依賴如下:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0</version>
</dependency>
程式碼如下:
DiaoSi javaBean:
package bean;
import java.io.Serializable;
public class DiaoSi implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private double age;
private String birthday;
private String school;
private boolean has_girlfriend;
private Object car;
private Object house;
private String[] major;
private String comment;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public boolean isHas_girlfriend() {
return has_girlfriend;
}
public void setHas_girlfriend(boolean has_girlfriend) {
this.has_girlfriend = has_girlfriend;
}
public Object getCar() {
return car;
}
public void setCar(Object car) {
this.car = car;
}
public Object getHouse() {
return house;
}
public void setHouse(Object house) {
this.house = house;
}
public String[] getMajor() {
return major;
}
public void setMajor(String[] major) {
this.major = major;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
wangxiaoer.json json資料檔案:
{
"name":"王小二",
"age":25.2,
"birthday":"1990-01-01",
"school":"藍翔",
"major":["理髮","挖掘機"],
"has_girlfriend":false,
"house":null,
"car":null,
"comment":"這是一個註釋"
}
從json資料檔案中讀取內容並解析:
// 返回的是當前Class這個類所在包開始的為置
File file = new File(ReadJsonSample.class.getResource("wangxiaoer.json").getFile());
String content = FileUtils.readFileToString(file);
JSONObject jsonObject = new JSONObject(content);
if(!jsonObject.isNull("name")){
System.out.println("姓名是:" + jsonObject.getString("name"));
}
if(!jsonObject.isNull("age")){
System.out.println("年齡是:" + jsonObject.getDouble("age"));
}
if(!jsonObject.isNull("major")){
JSONArray majorArray = jsonObject.getJSONArray("major");
for (Object object : majorArray) {
System.out.println(object.toString());
}
}
解析結果如下所示: