2014年10月20日 星期一

[Android] 分解JSON的資料

解JSON!!!解JSON!!!解JSON!!!解JSON!!!解JSON!!!解JSON!!!解JSON!!!
JSONArray是 "[" ,JSONOject是"{"


JSON 內容 - 1
[{"station":[{"longitude":116.346596,"latitude":39.930893,"radius":78}],
   "mark":[{"longitude":"116.346346","latitude":"39.930990"}],
   "data":[{"name":"张智隆","location_type":"0",
                "location":"北京市西城区百万庄南街10号院-5号楼","event":"用戶即時定位",
                "type":"26","watch_time":"2014-11-12 08:47",
                "server_time":"2014-11-12 08:48"}],
   "status":"0","msg":"2800"}]

程式碼內容 - 1
String Station = new JSONArray(data).getJSONObject(0).getString("station");
String Mark = new JSONArray(data).getJSONObject(0).getString("mark");
String Data = new JSONArray(data).getJSONObject(0).getString("data");
//Station
StationLatitude = new JSONArray(Station).getJSONObject(0).getDouble("latitude");
StationLongitude = new JSONArray(Station).getJSONObject(0).getDouble("longitude");
StationRadius = new JSONArray(Station).getJSONObject(0).getInt("radius");
//Mark
String MarkLatitude = new JSONArray(Mark).getJSONObject(0).getString("latitude");
String MarkLongitude = new JSONArray(Mark).getJSONObject(0).getString("longitude");
//Data
DataName = new JSONArray(Data).getJSONObject(0).getString("name");
DataLocationType = new JSONArray(Data).getJSONObject(0).getInt("location_type");
String DataLocation = new JSONArray(Data).getJSONObject(0).getString("location");
DataEvent = new JSONArray(Data).getJSONObject(0).getString("event");
String DataType = new JSONArray(Data).getJSONObject(0).getString("type");
DataWatchTime = new JSONArray(Data).getJSONObject(0).getString("watch_time");
DataServerTime = new JSONArray(Data).getJSONObject(0).getString("server_time");


抓取JSON裡的內容多少,還沒用過,不確定
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   JSONArray arr = new JSONArray(postResult); JSONObject obj = arr.getJSONObject(0); // Db.getInstance(this).deleteMsgSafeData(null); if(obj.optString("status").equals("0")){ arr = obj.getJSONArray("data"); for(int i=0; i<arr.length(); i++){ JSONObject msg = arr.getJSONObject(i); MessageSafe msgData = new MessageSafe(); msgData.setUserAccount(data.getUser_account());
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

參考網址
http://dean-android.blogspot.tw/2013/06/androidjson.html

沒有留言:

張貼留言