close
時間真是不夠用啊!! 有上課的時間只能緊抓回家後的一點點時間記錄一下,像蝸牛一樣前進吧!

設定的歷史交易查詢資料,來解析出來,過程是這樣:

1. 點擊"歷史交易"後,跳到HistoryActivity畫面,連上資料庫去查詢,當然啦! 不允許再UI Thread處理,所以要用AsyncTask來做,取得的資料,是一串String字串。




2. 於是,我們需要來解析這串字串的意義,就在上回Android開發者cosplay之第18話JSON陣列解析提到的JSON陣列型態,深入來說,每筆讀到的資料,都是多個JSONObject組成的JSONArray,而JSONObject就有我們需要讀到的屬性和對應值。

ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
JSONArray array = new JSONArray(line); 把讀到的字串line丟到JSONArray去SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 把資料型態變成2013-08-29這種樣子for (int i = 0; i<array.length(); i++){
JSONObject obj = array.getJSONObject(i); 從JSONArray解析出JSONObject,有數筆,需要逐筆讀出再從每筆讀出的JSONObject去得到需要的資料int amount = obj.getInt("amount");
String uid = obj.getString("userid");
long time = obj.getJSONObject("date").getLong("time"); 時間的JSON格式是long,需要透過SimpleDateFormat再去設定我們想要的時間格式Date date = new Date(time);
String d = sdf.format(date);
Log.d("HIST", amount+"/"+uid+"/"+time+"/"+d);
HashMap<String, String> map = new HashMap<String, String>();
map.put("date", d);
map.put("amount", amount+"");
map.put("userid", uid);
data.add(map);
有log結果和程式對照,很容易就能理解該怎麼下程式取解析。
arrow
arrow
    全站熱搜

    brianc18 發表在 痞客邦 留言(0) 人氣()