Android中JSON資料格式的簡單使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   os   使用   

/** * JSON:JavaScript對象標記法(JavaScript Object Notation)。 <br/> * JSON是儲存和交換文本資訊的文法。<br/> *  * 特點:<br/> * JSON是輕量級的文本資料交換格式<br/> * JSON獨立於語言和平台<br/> * JSON具有自我描述性,更以理解<br/> *  * 與XML的區別:<br/> * 類似XML,比XML更小、更快、更易解析<br/> * 沒有結束標籤<br/> * 更短<br/> * 讀寫的速度更快<br/> * 使用數組<br/> * 不使用保留字<br/> *  * JSON文法是JavaScript對象標記法文法的子集。<br/> * 資料在成對的名稱和數值中<br/> * 資料由逗號分隔<br/> * 花括弧儲存對象<br/> * 方括弧儲存數組<br/> *  * JSON值可以是:<br/> * 數字(正數或浮點數)<br/> * 字串(在雙引號中)<br/> * 邏輯值(true或false)<br/> * 數組(在方括弧中)<br/> * 對象(在花括弧中)<br/> * null<br/> *  * @author wangzhu *  */

 

1、從檔案中讀取JSON對象

/**     * 讀取Json對象     */    private void readJsonObject() {        String json = readFile();        try {            JSONObject root = new JSONObject(json);            System.err.println("cat=" + root.getString("cat"));            JSONArray array = root.getJSONArray("language");            for (int i = 0; i < array.length(); i++) {                System.err.println("------------");                JSONObject object = array.getJSONObject(i);                System.err.println("id=" + object.getInt("id"));                System.err.println("ide=" + object.getString("ide"));                System.err.println("name=" + object.getString("name"));            }        } catch (JSONException e) {            e.printStackTrace();        }    }    /**     * 讀取檔案中的內容,文本編碼方式為UTF-8     *      * @return     */    private String readFile() {        StringBuilder accum = new StringBuilder();        InputStreamReader isr = null;        BufferedReader br = null;        try {            isr = new InputStreamReader(getAssets().open("testJson.json"),                    "UTF-8");            br = new BufferedReader(isr);            String line = null;            while ((line = br.readLine()) != null) {                accum.append(line);            }        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (isr != null) {                try {                    isr.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return accum.toString();    }

Json檔案:

執行結果:

2、建立Json對象

/**     * 建立Json對象     */    private void createJsonObject() {        try {            JSONObject root = new JSONObject();            root.put("cat", "it");            JSONArray array = new JSONArray();            JSONObject lan1 = new JSONObject();            lan1.put("id", 1);            lan1.put("ide", "Eclipse");            lan1.put("name", "Java");            array.put(lan1);            JSONObject lan2 = new JSONObject();            lan2.put("id", 2);            lan2.put("ide", "Xcode");            lan2.put("name", "Swift");            array.put(lan2);            JSONObject lan3 = new JSONObject();            lan3.put("id", 3);            lan3.put("ide", "Visual Studio");            lan3.put("name", "C#");            array.put(lan3);            root.put("language", array);            System.err.println("createJsonObject: " + root.toString());        } catch (JSONException e) {            e.printStackTrace();        }    }

執行結果:

Android中JSON資料格式的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.