1. 程式人生 > >Java讀取json檔案並對json資料進行讀取、新增、刪除與修改操作

Java讀取json檔案並對json資料進行讀取、新增、刪除與修改操作

1.介紹

開發過程中經常會遇到json資料的處理,而單獨對json資料進行增刪改並不方便,尤其是Geojson檔案的處理,通過對網路資料的整理總結,下面介紹Java語言方法對json資料進行讀取、新增、刪除與修改操作。

2.說明

Java語言操作json物件,需引入json資料操作庫(org.json.jar)檔案,可通過網路搜尋尋找,另外本文附件程式碼中已包含,在Eclipse或其它編譯工具中直接引入即可。

本文通過一個Geojson檔案(HK_geo.json)為例進行介紹,nameID.txt為新json新新增欄位(key)所對應的值(value)。HK_new.json為修改後的新檔案。

3.json樣例

{
	"type": "FeatureCollection",
	"features": [{
		"type": "Feature",
		"properties": {
			"name": "Yuen Long",
			"ID_0": 102,
			"ID_1": 18,
			"ISO": "HKG"
		},
		"geometry": {
			"type": "Polygon",
			"coordinates": [[[114.084511, 22.519991], [114.075668, 22.517466], [114.078194, 22.516203], [114.079460, 22.516623], [114.082825, 22.519150], [114.084511, 22.519991]]]
		}
	}]
}

4.程式碼

package json;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

public class JsonConvert {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 讀取nameID.txt檔案中的NAMEID欄位(key)對應值(value)並存儲
		ArrayList<String> list = new ArrayList<String>();
		BufferedReader brname;
		try {
			brname = new BufferedReader(new FileReader("src/json/nameID.txt"));// 讀取NAMEID對應值
			String sname = null;
			while ((sname = brname.readLine()) != null) {
				// System.out.println(sname);
				list.add(sname);// 將對應value新增到連結串列儲存
			}
			brname.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 讀取原始json檔案並進行操作和輸出
		try {
			BufferedReader br = new BufferedReader(new FileReader(
					"src/json/HK_geo.json"));// 讀取原始json檔案
			BufferedWriter bw = new BufferedWriter(new FileWriter(
					"src/json/HK_new.json"));// 輸出新的json檔案
			String s = null, ws = null;
			while ((s = br.readLine()) != null) {
				// System.out.println(s);
				try {
					JSONObject dataJson = new JSONObject(s);// 建立一個包含原始json串的json物件
					JSONArray features = dataJson.getJSONArray("features");// 找到features的json陣列
					for (int i = 0; i < features.length(); i++) {
						JSONObject info = features.getJSONObject(i);// 獲取features陣列的第i個json物件
						JSONObject properties = info.getJSONObject("properties");// 找到properties的json物件
						String name = properties.getString("name");// 讀取properties物件裡的name欄位值
						System.out.println(name);
						properties.put("NAMEID", list.get(i));// 新增NAMEID欄位
						// properties.append("name", list.get(i));
						System.out.println(properties.getString("NAMEID"));
						properties.remove("ISO");// 刪除ISO欄位
					}
					ws = dataJson.toString();
					System.out.println(ws);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			bw.write(ws);
			// bw.newLine();

			bw.flush();
			br.close();
			bw.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

5.原始碼下載