1. 程式人生 > >使用org.json.JSONObject處理Json資料

使用org.json.JSONObject處理Json資料

引入org.json依賴

在 maven 專案中使用 org.json ,需引入依賴:

		<!-- 引入org.json所需依賴 -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20160810</version>
		</dependency>

構建JSONObject

直接構建

可以直接使用 new 關鍵字例項化一個JSONObject物件,然後呼叫它的 put() 方法對其欄位值進行設定。

 範例:

		JSONObject jsonObj = new JSONObject();
		jsonObj.put("female", true);
		jsonObj.put("hobbies", Arrays.asList(new String[] { "yoga", "swimming" }));
		jsonObj.put("discount", 9.5);
		jsonObj.put("age", "26");
		jsonObj.put("features", new HashMap<String, Integer>() {
			private static final long serialVersionUID = 1L;
			{
				put("height", 175);
				put("weight", 70);
			}
		});
		System.out.println(jsonObj);

結果:

{
	"features": {
		"weight": 70,
		"height": 175
	},
	"hobbies": ["yoga", "swimming"],
	"discount": 9.5,
	"female": true,
	"age": 26
}

使用Map構建

範例:

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("female", true);
		map.put("hobbies", Arrays.asList(new String[] { "yoga", "swimming" }));
		map.put("discount", 9.5);
		map.put("age", "26");
		map.put("features", new HashMap<String, Integer>() {
			private static final long serialVersionUID = 1L;
			{
				put("height", 175);
				put("weight", 70);
			}
		});
		JSONObject jsonObj = new JSONObject(map);
		System.out.println(jsonObj);

程式執行結果與上例相同。

使用JavaBean構建

範例:


import java.util.Map;

public class UserInfo {

	private Boolean female;
	private String[] hobbies;
	private Double discount;
	private Integer age;
	private Map<String, Integer> features;

	public Boolean getFemale() {
		return female;
	}

	public void setFemale(Boolean female) {
		this.female = female;
	}

	public String[] getHobbies() {
		return hobbies;
	}

	public void setHobbies(String[] hobbies) {
		this.hobbies = hobbies;
	}

	public Double getDiscount() {
		return discount;
	}

	public void setDiscount(Double discount) {
		this.discount = discount;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Map<String, Integer> getFeatures() {
		return features;
	}

	public void setFeatures(Map<String, Integer> features) {
		this.features = features;
	}

}
		UserInfo userInfo = new UserInfo();
		userInfo.setFemale(true);
		userInfo.setHobbies(new String[] { "yoga", "swimming" });
		userInfo.setDiscount(9.5);
		userInfo.setAge(26);
		userInfo.setFeatures(new HashMap<String, Integer>() {
			private static final long serialVersionUID = 1L;
			{
				put("height", 175);
				put("weight", 70);
			}
		});
		JSONObject jsonObj = new JSONObject(userInfo);
		System.out.println(jsonObj);

程式執行結果與上例相同。

解析JSONObject

JSONObject為每一種資料型別都提供了一個getXXX(key)方法,例如:獲取字串型別的欄位值就使用getString()方法,獲取陣列型別的欄位值就使用getJSONArray()方法。

 範例:

		// 獲取基本型別資料
		System.out.println("Female: " + jsonObj.getBoolean("female"));
		System.out.println("Discount: " + jsonObj.getDouble("discount"));
		System.out.println("Age: " + jsonObj.getLong("age"));
		
		// 獲取JSONObject型別資料
		JSONObject features = jsonObj.getJSONObject("features");
		String[] names = JSONObject.getNames(features);
		System.out.println("Features: ");
		for (int i = 0; i < names.length; i++) {
			System.out.println("\t"+features.get(names[i]));
		}

		// 獲取陣列型別資料
		JSONArray hobbies = jsonObj.getJSONArray("hobbies");
		System.out.println("Hobbies: ");
		for (int i = 0; i < hobbies.length(); i++) {
			System.out.println("\t"+hobbies.get(i));
		}

結果:

Female: true
Discount: 9.5
Age: 26
Features: 
	70
	175
Hobbies: 
	yoga
	swimming