JSON Programming Javaorg json File New Java Project Slides: 23 Download presentation JSON Programming 使用Java+org. json套件存取開放資料 File -> New -> Java Project JSON剖析 具的網路載點 (https: //github. com/stleary/JSON-java) 在Eclipse加入JSON剖析 具(1/2) 專案名稱->Build Path->Add External Archives 在Eclipse加入JSON剖析 具(2/2) JSON JSONObject for 單列資料表 { name chinese english female weight amanda 85 90 true 52. 5 } "name" : "amanda", "chinese": 85, "english" : 90, "female" : true, "weight" : 52. 5 建立JSONObject 方法(1)—應用 put() • JSONObject obj = new JSONObject(); • obj. put(param 1, param 2) • param 1 (String): 欄位名稱 • param 2 (int/double/String/boolean): 欄位值 { } "name" : "amanda", "chinese": 85, "english" : 90, "weight" : 52. 5, "female" : true 建立JSONObject 方法(2)—JSON字串作為資料來源 • JSONObject obj = new JSONObject(param); • param (String): JSON字串 { } "name" : "amanda", "chinese": 85, "english" : 90, "weight" : 52. 5, "female" : true JSONArray for 單欄多值資料 chinese 85 77 96 82 [ 91 ] 85, 77, 96, 82, , 91, 建立JSONArray 方法(1)—應用 put() • JSONArray array = new JSONArray(); • array. put(param) • param (int/double/String/boolean): 欄位值 [ ] 85, 77, 96, 82, , 91, 建立JSONArray 方法(2)—JSON字串作為資料來源 • JSONArray array = new JSONArray(param); • param (String): JSON字串 chinese 85 77 96 82 91 請刪除 null 試試看 讀取文字檔 Scanner建構式 拋出File. Not. Found. Exception 多列資料表 name amanda bob carol chinese 85 92 83 english 90 76 94 female weight true 52. 5 false 77. 3 true 多列資料表的 JSON 表示法 [ { "name" : "amanda", "chinese": 85, "english" : 90, "female" : true, "weight" : 52. 5}, { "name" : "bob", "chinese": 92, "english" : 76, "female" : false, "weight" : 77. 3}, { "name" : "carol", "chinese": 83, "english" : 94, "female" : true, "weight" : null } ]