安卓學習筆記之原生Json

什麼是json?

json全稱就是JavaScript Object Notation,是一種輕量級的數據交換格式,以一種鍵值(key,value)的形式存在。那安卓上是怎麼實現創建和讀取的呢?下面來學習學習。

首先是創建json格式,一般我們都會用到JsonObeject這個類,創建其對象。

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.text1);

       try {
            JSONObject root = new JSONObject();
            JSONObject jsonObject1 = new JSONObject();
            JSONObject jsonObject2 = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            //一個json對象  
            jsonObject1.put("name","xixi");
            jsonObject1.put("age",18);
            jsonObject1.put("sex","male");
            //第二個json對象
            jsonObject2.put("name","haha");
            jsonObject2.put("age",19);
            jsonObject2.put("sex","male");
            //json數組
            jsonArray.put(jsonObject1);
            jsonArray.put(jsonObject2);
            //將json數組添加到root這個jsonObject這個類的對象裏去,並指定它的鍵爲handsome
            root.put("handsome",jsonArray);

            System.out.println(root.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
這樣就創建了json格式,我們通過控制檯看到打印的內容。

那如何通過網絡讀取json數據呢?我們貼上代碼。

//在安卓3.0之後google要求網絡請求要在子線程中進行
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                try {
                    //輸入流,並指定其url地址,打開流
                    InputStream in = new URL("http://aqicn.org/publishingdata/json").openStream();
                    //通過BufferedReader來入去inputStream,其中inputStreamReader來封裝inputStream
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String line;
                    StringBuffer content = new StringBuffer();
                    while ((line = br.readLine()) != null) {
                        content.append(line);
                    }
                    //讀取完成之後關閉BufferedReader
                    br.close();
                    return content.toString();
                    
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }

            //當網絡操作成功連接之後調用這個方法
            @Override
            protected void onPostExecute(String s) {
                //s爲從網絡讀取的數據
                if (s!=null){
                    try {
                        //創建一個數組對象,放入s內容
                        JSONArray jo= new JSONArray(s);
                        JSONObject jso = jo.getJSONObject(0);
                        //從網絡中取到鍵爲pollutants這個鍵然後讀取其內容
                        JSONArray pollutants = jso.getJSONArray("pollutants");
                        //控制檯輸出
                        System.out.println("cityName:  "+jso.getString("cityName"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.execute();
如圖所示,我們已經順利讀取了網絡的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章