1. 程式人生 > >AS中okhttp環境下的json解析

AS中okhttp環境下的json解析

Json簡介:

可參考部落格 菜鳥教程json簡介 這裡面講的很清楚,我就不囉嗦了。
解析json資料格式的方法有多種,我跟大家分享我學習的兩種:“安卓原生”解析和“gson框架”解析。

以下程式碼建立在上篇文章okhttp的post和get請求之上,在非同步get請求的方法中繼續新增程式碼。

安卓原生解析

先看我從網上api中獲得的json解析欄位,以下就是將改欄位顯示到textView的操作。
json原文

private String part1(String jsonData)  //建立一個類傳入得到的json字串
    {
        JSONObject obj = null
; //建立json物件 String forResult; forResult = "not Found!"; //如果沒有輸出字元,則顯示forResult if (!jsonData.isEmpty()) { try //加上異常處理 { obj = new JSONObject(jsonData); //將字串轉為json物件 forResult = obj.optString("result");//把result物件提取出來,並轉化為字串
} catch (JSONException e) { e.printStackTrace(); } } return forResult; } //利用原生解析json陣列[] private void part2(String jsonData) //傳入純陣列形式(沒有陣列名的陣列,去掉result頭部)的字串 { try { JSONArray jsonArray= new
JSONArray(jsonData); //將json字串轉為jsonArray物件 for (int i=0;i<jsonArray.length();i++) //將json陣列一組一組的輸出 { JSONObject jsonObject = jsonArray.getJSONObject(i); if (null != jsonObject) { String title = "標題:"+jsonObject.optString("title"); //括號中的字串一定要在json陣列中存在 String content = "正文:"+jsonObject.optString("content"); String type = "型別:"+jsonObject.optString("type"); String updatetime = "更新時間:"+jsonObject.optString("updatetime"); text.append(title+"\n"+content+"\n"+type+"\n"+updatetime+"\n\n"); //輸出到text中 } } } catch (JSONException e) { e.printStackTrace(); } } //在MainActivity中呼叫 if(response.isSuccessful()){ final String result=response.body().string(); //得到返回的json字串 runOnUiThread(new Runnable() { @Override public void run() { String forResult = part1(result); //得到result所包含的純陣列,沒有陣列頭 part2(forResult); } }); }

原生解析執行結果
原生解析就結束了。

Gson框架解析:

對於每個外層的{ }物件都要寫一個JavaBean類來宣告他裡面含有的元素,陣列也同樣,所以根據json資料我需要兩個javaBean類,可以自定義,我定義了兩個類:Person.class作為最外層json物件的元素宣告,Result.class作為result名下陣列的宣告。
//第一個clas檔案

public class Person
{
    private int error_code;
    private String reason;
    private List<Result> result; //result是陣列所以用集合泛型的形式宣告
//構造
    Person(int error_code,String reason,List<Result> result)
    {
        this.error_code = error_code;
        this.reason = reason;
        this.result = result;
    }

    int getError_code()
    {
        return error_code;
    }

    String getReason()
    {
        return reason;
    }

    List<Result> getResult()
    {
        return result;
    }

    @Override //一定要加個toString才會轉化為String字串的形式輸出
    public String toString()
    {
        return "result:"+result;
    }
}

//第二個class檔案

public class Result {
    private String title;
    private String content;
    private String type;
    private String updatetime;
    //構造
    Result(String title,String content,String type,String updatetime)
    {
        this.title = title;
        this.content = content;
        this.type = type;
        this.updatetime = updatetime;
    }

    String getTitle()
    {
        return title;
    }
    String getContent()
    {
        return content;
    }
    String getType()
    {
        return type;
    }
    String getUpdatetime()
    {
        return updatetime;
    }
    //一定要加toString
    @Override
    public String toString()
    {
        return  "\n標題:"+title+
                "\n正文:"+content+
                "\n型別:"+type+
                "\n更新時間:"+updatetime;
    }
}

Gson框架解析執行結果

這裡面講的是對於比較複雜的json資料進行解析,基礎是簡單json資料,所以遇到更復雜的資料,也要依據基礎來一層一層解析。