iT邦幫忙

0

第40天~還是JSON

Tzu 2022-02-12 14:07:311360 瀏覽
  • 分享至 

  • xImage
  •  

這篇的上一篇:https://ithelp.ithome.com.tw/articles/10283960

這裡要來寫....還是JSON
開新檔-發現模擬器要在512MB下開才跑得比較快

https://ithelp.ithome.com.tw/upload/images/20220213/20119035Fxbr1sUMSO.png

然後這裡是抓下來的JSON資料:

{\"SiteName\":\"二林\",\"County\":\"彰化縣\",\"AQI\":\"37\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1\",\"CO\":\"0.25\",\"CO_8hr\":\"0.3\",\"O3\":\"27.6\",\"O3_8hr\":\"29\",\"PM10\":\"24\",\"PM2.5\":\"8\",\"NO2\":\"5\",\"NOx\":\"6.5\",\"NO\":\"1.6\",\"WindSpeed\":\"6.4\",\"WindDirec\":\"25\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"12\",\"PM10_AVG\":\"30\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.409653\",\"Latitude\":\"23.925175\",\"SiteId\":\"35\"},\n" +
            "{\"SiteName\":\"三重\",\"County\":\"新北市\",\"AQI\":\"59\",\"Pollutant\":\"細懸浮微粒\",\"Status\":\"普通\",\"SO2\":\"4.9\",\"CO\":\"3.13\",\"CO_8hr\":\"2.2\",\"O3\":\"-\",\"O3_8hr\":\"-\",\"PM10\":\"23\",\"PM2.5\":\"16\",\"NO2\":\"49\",\"NOx\":\"208.4\",\"NO\":\"159.4\",\"WindSpeed\":\"-\",\"WindDirec\":\"-\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"19\",\"PM10_AVG\":\"29\",\"SO2_AVG\":\"3\",\"Longitude\":\"121.493806\",\"Latitude\":\"25.072611\",\"SiteId\":\"67\"},\n" +
            "{\"SiteName\":\"三義\",\"County\":\"苗栗縣\",\"AQI\":\"26\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1.9\",\"CO\":\"0.25\",\"CO_8hr\":\"0.2\",\"O3\":\"21.3\",\"O3_8hr\":\"26\",\"PM10\":\"11\",\"PM2.5\":\"3\",\"NO2\":\"11.6\",\"NOx\":\"12.7\",\"NO\":\"1.2\",\"WindSpeed\":\"6\",\"WindDirec\":\"20\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"8\",\"PM10_AVG\":\"12\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.758833\",\"Latitude\":\"24.382942\",\"SiteId\":\"27\"}
            

放入JAVA 檔-

https://ithelp.ithome.com.tw/upload/images/20220213/20119035rIALM8TkPb.png

package com.huang.jsonagain1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    String json="[]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

再加入JSON資料

https://ithelp.ithome.com.tw/upload/images/20220213/20119035KFw3P3QBPW.png

package com.huang.jsonagain1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    String json="[{\\\"SiteName\\\":\\\"二林\\\",\\\"County\\\":\\\"彰化縣\\\",\\\"AQI\\\":\\\"37\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.3\\\",\\\"O3\\\":\\\"27.6\\\",\\\"O3_8hr\\\":\\\"29\\\",\\\"PM10\\\":\\\"24\\\",\\\"PM2.5\\\":\\\"8\\\",\\\"NO2\\\":\\\"5\\\",\\\"NOx\\\":\\\"6.5\\\",\\\"NO\\\":\\\"1.6\\\",\\\"WindSpeed\\\":\\\"6.4\\\",\\\"WindDirec\\\":\\\"25\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"12\\\",\\\"PM10_AVG\\\":\\\"30\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.409653\\\",\\\"Latitude\\\":\\\"23.925175\\\",\\\"SiteId\\\":\\\"35\\\"},\\n\" +\n" +
            "            \"{\\\"SiteName\\\":\\\"三重\\\",\\\"County\\\":\\\"新北市\\\",\\\"AQI\\\":\\\"59\\\",\\\"Pollutant\\\":\\\"細懸浮微粒\\\",\\\"Status\\\":\\\"普通\\\",\\\"SO2\\\":\\\"4.9\\\",\\\"CO\\\":\\\"3.13\\\",\\\"CO_8hr\\\":\\\"2.2\\\",\\\"O3\\\":\\\"-\\\",\\\"O3_8hr\\\":\\\"-\\\",\\\"PM10\\\":\\\"23\\\",\\\"PM2.5\\\":\\\"16\\\",\\\"NO2\\\":\\\"49\\\",\\\"NOx\\\":\\\"208.4\\\",\\\"NO\\\":\\\"159.4\\\",\\\"WindSpeed\\\":\\\"-\\\",\\\"WindDirec\\\":\\\"-\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"19\\\",\\\"PM10_AVG\\\":\\\"29\\\",\\\"SO2_AVG\\\":\\\"3\\\",\\\"Longitude\\\":\\\"121.493806\\\",\\\"Latitude\\\":\\\"25.072611\\\",\\\"SiteId\\\":\\\"67\\\"},\\n\" +\n" +
            "            \"{\\\"SiteName\\\":\\\"三義\\\",\\\"County\\\":\\\"苗栗縣\\\",\\\"AQI\\\":\\\"26\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1.9\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.2\\\",\\\"O3\\\":\\\"21.3\\\",\\\"O3_8hr\\\":\\\"26\\\",\\\"PM10\\\":\\\"11\\\",\\\"PM2.5\\\":\\\"3\\\",\\\"NO2\\\":\\\"11.6\\\",\\\"NOx\\\":\\\"12.7\\\",\\\"NO\\\":\\\"1.2\\\",\\\"WindSpeed\\\":\\\"6\\\",\\\"WindDirec\\\":\\\"20\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"8\\\",\\\"PM10_AVG\\\":\\\"12\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.758833\\\",\\\"Latitude\\\":\\\"24.382942\\\",\\\"SiteId\\\":\\\"27\\\"}\n" +
            "            ]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

要用JSONArray 然後再修正錯誤-->try catch

https://ithelp.ithome.com.tw/upload/images/20220213/20119035rCh8dOAcR7.png

package com.huang.jsonagain1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import org.json.JSONArray;

public class MainActivity extends AppCompatActivity {
    String json="[{\\\"SiteName\\\":\\\"二林\\\",\\\"County\\\":\\\"彰化縣\\\",\\\"AQI\\\":\\\"37\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.3\\\",\\\"O3\\\":\\\"27.6\\\",\\\"O3_8hr\\\":\\\"29\\\",\\\"PM10\\\":\\\"24\\\",\\\"PM2.5\\\":\\\"8\\\",\\\"NO2\\\":\\\"5\\\",\\\"NOx\\\":\\\"6.5\\\",\\\"NO\\\":\\\"1.6\\\",\\\"WindSpeed\\\":\\\"6.4\\\",\\\"WindDirec\\\":\\\"25\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"12\\\",\\\"PM10_AVG\\\":\\\"30\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.409653\\\",\\\"Latitude\\\":\\\"23.925175\\\",\\\"SiteId\\\":\\\"35\\\"},\\n\" +\n" +
            "            \"{\\\"SiteName\\\":\\\"三重\\\",\\\"County\\\":\\\"新北市\\\",\\\"AQI\\\":\\\"59\\\",\\\"Pollutant\\\":\\\"細懸浮微粒\\\",\\\"Status\\\":\\\"普通\\\",\\\"SO2\\\":\\\"4.9\\\",\\\"CO\\\":\\\"3.13\\\",\\\"CO_8hr\\\":\\\"2.2\\\",\\\"O3\\\":\\\"-\\\",\\\"O3_8hr\\\":\\\"-\\\",\\\"PM10\\\":\\\"23\\\",\\\"PM2.5\\\":\\\"16\\\",\\\"NO2\\\":\\\"49\\\",\\\"NOx\\\":\\\"208.4\\\",\\\"NO\\\":\\\"159.4\\\",\\\"WindSpeed\\\":\\\"-\\\",\\\"WindDirec\\\":\\\"-\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"19\\\",\\\"PM10_AVG\\\":\\\"29\\\",\\\"SO2_AVG\\\":\\\"3\\\",\\\"Longitude\\\":\\\"121.493806\\\",\\\"Latitude\\\":\\\"25.072611\\\",\\\"SiteId\\\":\\\"67\\\"},\\n\" +\n" +
            "            \"{\\\"SiteName\\\":\\\"三義\\\",\\\"County\\\":\\\"苗栗縣\\\",\\\"AQI\\\":\\\"26\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1.9\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.2\\\",\\\"O3\\\":\\\"21.3\\\",\\\"O3_8hr\\\":\\\"26\\\",\\\"PM10\\\":\\\"11\\\",\\\"PM2.5\\\":\\\"3\\\",\\\"NO2\\\":\\\"11.6\\\",\\\"NOx\\\":\\\"12.7\\\",\\\"NO\\\":\\\"1.2\\\",\\\"WindSpeed\\\":\\\"6\\\",\\\"WindDirec\\\":\\\"20\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"8\\\",\\\"PM10_AVG\\\":\\\"12\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.758833\\\",\\\"Latitude\\\":\\\"24.382942\\\",\\\"SiteId\\\":\\\"27\\\"}\n" +
            "            ]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JSONArray jsonArray = new JSONArray(json);
    }
}

然後再來修正裡面的內容變成: Log.e("Howard","JSONException"+e);

https://ithelp.ithome.com.tw/upload/images/20220213/20119035fSfDT3WEQQ.png

package com.huang.jsonagain1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;

public class MainActivity extends AppCompatActivity {
    String json="[{\\\"SiteName\\\":\\\"二林\\\",\\\"County\\\":\\\"彰化縣\\\",\\\"AQI\\\":\\\"37\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.3\\\",\\\"O3\\\":\\\"27.6\\\",\\\"O3_8hr\\\":\\\"29\\\",\\\"PM10\\\":\\\"24\\\",\\\"PM2.5\\\":\\\"8\\\",\\\"NO2\\\":\\\"5\\\",\\\"NOx\\\":\\\"6.5\\\",\\\"NO\\\":\\\"1.6\\\",\\\"WindSpeed\\\":\\\"6.4\\\",\\\"WindDirec\\\":\\\"25\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"12\\\",\\\"PM10_AVG\\\":\\\"30\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.409653\\\",\\\"Latitude\\\":\\\"23.925175\\\",\\\"SiteId\\\":\\\"35\\\"},\\n\" +\n" +
            "            \"{\\\"SiteName\\\":\\\"三重\\\",\\\"County\\\":\\\"新北市\\\",\\\"AQI\\\":\\\"59\\\",\\\"Pollutant\\\":\\\"細懸浮微粒\\\",\\\"Status\\\":\\\"普通\\\",\\\"SO2\\\":\\\"4.9\\\",\\\"CO\\\":\\\"3.13\\\",\\\"CO_8hr\\\":\\\"2.2\\\",\\\"O3\\\":\\\"-\\\",\\\"O3_8hr\\\":\\\"-\\\",\\\"PM10\\\":\\\"23\\\",\\\"PM2.5\\\":\\\"16\\\",\\\"NO2\\\":\\\"49\\\",\\\"NOx\\\":\\\"208.4\\\",\\\"NO\\\":\\\"159.4\\\",\\\"WindSpeed\\\":\\\"-\\\",\\\"WindDirec\\\":\\\"-\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"19\\\",\\\"PM10_AVG\\\":\\\"29\\\",\\\"SO2_AVG\\\":\\\"3\\\",\\\"Longitude\\\":\\\"121.493806\\\",\\\"Latitude\\\":\\\"25.072611\\\",\\\"SiteId\\\":\\\"67\\\"},\\n\" +\n" +
            "            \"{\\\"SiteName\\\":\\\"三義\\\",\\\"County\\\":\\\"苗栗縣\\\",\\\"AQI\\\":\\\"26\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1.9\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.2\\\",\\\"O3\\\":\\\"21.3\\\",\\\"O3_8hr\\\":\\\"26\\\",\\\"PM10\\\":\\\"11\\\",\\\"PM2.5\\\":\\\"3\\\",\\\"NO2\\\":\\\"11.6\\\",\\\"NOx\\\":\\\"12.7\\\",\\\"NO\\\":\\\"1.2\\\",\\\"WindSpeed\\\":\\\"6\\\",\\\"WindDirec\\\":\\\"20\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"8\\\",\\\"PM10_AVG\\\":\\\"12\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.758833\\\",\\\"Latitude\\\":\\\"24.382942\\\",\\\"SiteId\\\":\\\"27\\\"}\n" +
            "            ]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            JSONArray jsonArray = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("Howard","JSONException"+e);
        }
    }
}

然後因為要讀裡面的資料要用FOR迴圈-->然後取出-->最後輸出

https://ithelp.ithome.com.tw/upload/images/20220213/201190350rJRIUbdgl.png

package com.huang.jsonagain1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;



public class MainActivity extends AppCompatActivity {
    String json = "[{\"SiteName\":\"二林\",\"County\":\"彰化縣\",\"AQI\":\"37\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1\",\"CO\":\"0.25\",\"CO_8hr\":\"0.3\",\"O3\":\"27.6\",\"O3_8hr\":\"29\",\"PM10\":\"24\",\"PM2.5\":\"8\",\"NO2\":\"5\",\"NOx\":\"6.5\",\"NO\":\"1.6\",\"WindSpeed\":\"6.4\",\"WindDirec\":\"25\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"12\",\"PM10_AVG\":\"30\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.409653\",\"Latitude\":\"23.925175\",\"SiteId\":\"35\"},\n" +
            "{\"SiteName\":\"三重\",\"County\":\"新北市\",\"AQI\":\"59\",\"Pollutant\":\"細懸浮微粒\",\"Status\":\"普通\",\"SO2\":\"4.9\",\"CO\":\"3.13\",\"CO_8hr\":\"2.2\",\"O3\":\"-\",\"O3_8hr\":\"-\",\"PM10\":\"23\",\"PM2.5\":\"16\",\"NO2\":\"49\",\"NOx\":\"208.4\",\"NO\":\"159.4\",\"WindSpeed\":\"-\",\"WindDirec\":\"-\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"19\",\"PM10_AVG\":\"29\",\"SO2_AVG\":\"3\",\"Longitude\":\"121.493806\",\"Latitude\":\"25.072611\",\"SiteId\":\"67\"},\n" +
            "{\"SiteName\":\"三義\",\"County\":\"苗栗縣\",\"AQI\":\"26\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1.9\",\"CO\":\"0.25\",\"CO_8hr\":\"0.2\",\"O3\":\"21.3\",\"O3_8hr\":\"26\",\"PM10\":\"11\",\"PM2.5\":\"3\",\"NO2\":\"11.6\",\"NOx\":\"12.7\",\"NO\":\"1.2\",\"WindSpeed\":\"6\",\"WindDirec\":\"20\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"8\",\"PM10_AVG\":\"12\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.758833\",\"Latitude\":\"24.382942\",\"SiteId\":\"27\"}]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length();i++){
                JSONObject jObj =  jsonArray.getJSONObject(i);
                String siteName = jObj.getString("SiteName");
                String county =  jObj.getString("County");
                String aqi =  jObj.getString("AQI");
                //把JSON資料轉換為物件

                Log.d("Howard","siteName:"+siteName+
                        " county:"+county+" aqi:"+aqi);


            }
        } catch (JSONException e) {
            Log.e("Howard","JSONException:"+e);
        }


    }
}

然後也是從log看~

https://ithelp.ithome.com.tw/upload/images/20220213/20119035Un7P04jxUA.png


轉成VIEW看-
先新增PACKAGE-bean

https://ithelp.ithome.com.tw/upload/images/20220213/20119035dKHUu5jVrP.png

https://ithelp.ithome.com.tw/upload/images/20220213/20119035cDYvroKY0C.png

裡面再新增CLASS-PmObj

https://ithelp.ithome.com.tw/upload/images/20220213/20119035EqIImdKU1O.png

對照前面的log的小寫填入:

Log.d("Howard","siteName:"+siteName+
                        " county:"+county+" aqi:"+aqi);

https://ithelp.ithome.com.tw/upload/images/20220213/20119035L0zbZ9x3ik.png

package com.huang.jsonagain1.bean;

public class PmObj {
    private String siteName;
    private String county;
    private String aqi;
            
}

然後按右鍵 再加上generate

https://ithelp.ithome.com.tw/upload/images/20220213/20119035cVydxBnMv7.png

設定getter+setter

一個為getter用來取得private varible,另一個則為setter用來設定private varible

https://dotblogs.com.tw/law1009/2012/01/06/64665

https://ithelp.ithome.com.tw/upload/images/20220213/20119035bD80QWGMjQ.png

然後按SHIFT+3個都選

https://ithelp.ithome.com.tw/upload/images/20220213/20119035quggbZkbVw.png

package com.huang.jsonagain1.bean;

public class PmObj {
    private String siteName;
    private String county;
    private String aqi;

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getAqi() {
        return aqi;
    }

    public void setAqi(String aqi) {
        this.aqi = aqi;
    }
}

然後按右鍵 再加上generate-設定Constructor

https://ithelp.ithome.com.tw/upload/images/20220213/20119035q1Fmnf7e1n.png

https://ithelp.ithome.com.tw/upload/images/20220213/201190356zsRzC1azC.png

然後也是按shift+3個全選
https://ithelp.ithome.com.tw/upload/images/20220213/20119035a08W9aHsgi.png

https://ithelp.ithome.com.tw/upload/images/20220213/20119035MiRdifBoIZ.png

package com.huang.jsonagain1.bean;

public class PmObj {
    private String siteName;
    private String county;
    private String aqi;

    public PmObj(String siteName, String county, String aqi) {
        this.siteName = siteName;
        this.county = county;
        this.aqi = aqi;
    }

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getAqi() {
        return aqi;
    }

    public void setAqi(String aqi) {
        this.aqi = aqi;
    }
}

然後再到最下面去toString按右鍵 再加上generate-去toString

https://ithelp.ithome.com.tw/upload/images/20220213/20119035ykV8VsgSfA.png

去toString

https://ithelp.ithome.com.tw/upload/images/20220213/20119035g4fud22Psu.png

然後也是全選

https://ithelp.ithome.com.tw/upload/images/20220213/20119035uldMr6wTvb.png

出來長這樣
https://ithelp.ithome.com.tw/upload/images/20220213/20119035GXiJjlh2jm.png

package com.huang.jsonagain1.bean;

public class PmObj {
    private String siteName;
    private String county;
    private String aqi;

    public PmObj(String siteName, String county, String aqi) {
        this.siteName = siteName;
        this.county = county;
        this.aqi = aqi;
    }

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getAqi() {
        return aqi;
    }

    public void setAqi(String aqi) {
        this.aqi = aqi;
    }

    @Override
    public String toString() {
        return "PmObj{" +
                "siteName='" + siteName + '\'' +
                ", county='" + county + '\'' +
                ", aqi='" + aqi + '\'' +
                '}';
    }
}


再回到MainActivity.java檔裡面加入 ArrayList

private ArrayList pmList = new ArrayList();

https://ithelp.ithome.com.tw/upload/images/20220213/20119035ygVypN6IXx.png

然後把另外一個檔案加入:

https://ithelp.ithome.com.tw/upload/images/20220213/20119035wypzyGfhpK.png

https://ithelp.ithome.com.tw/upload/images/20220213/20119035YPJspKceQB.png

把JSON資料轉換為物件-自己手KEY

https://ithelp.ithome.com.tw/upload/images/20220213/20119035SQoF9xjUQJ.png

https://ithelp.ithome.com.tw/upload/images/20220213/20119035zS6njGv2Ph.png

然後再貼入內容siteName,county,aqi:

https://ithelp.ithome.com.tw/upload/images/20220213/20119035tp3olBPjhF.png

自己手KEY--pmList.add(pmObj);

https://ithelp.ithome.com.tw/upload/images/20220213/20119035o23sNznRhi.png

然後跑看看
https://ithelp.ithome.com.tw/upload/images/20220213/201190355XUhAZrYBR.png

這篇的下一篇:https://ithelp.ithome.com.tw/articles/10283969


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言