JAVA 讀/寫 Json文件

(一)Json的優缺點java

  1. 優勢:做爲一種數據傳輸格式,JSON 與 XML 很類似,可是它更加靈巧;JSON不須要從服務器端發送含 有特定內容類型的首部信息。
  2. 缺點:語法過於嚴謹;代碼不易讀;eval 函數存在風險。

(二)JSON文件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]
                           ]]  
        }  
    }]  
}

(二) 讀寫Json文件的數據數組

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) {
                    e.printStackTrace();  
                }  
            }  
  
            bw.write(ws);  
            bw.newLine();  
            bw.flush();  
            br.close();  
            bw.close();  
  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
    }  
  
}
相關文章
相關標籤/搜索