亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在獲取整個 JSON 數據源的同時獲取某個標簽

如何在獲取整個 JSON 數據源的同時獲取某個標簽

喵喵時光機 2022-05-25 15:37:22
我想在 JSON 文件中記錄一些標簽的值。這是我的數據源:http ://data.nba.net/10s/prod/v1/2016/players.json我設法使用此處找到的代碼獲取整個數據流:使用 Android 從 URL 獲取 JSON 數據?我發布它是為了讓您更容易檢查我的代碼:    Button btnHit;TextView txtJson;ProgressDialog pd;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    btnHit = (Button) findViewById(R.id.btnHit);    txtJson = (TextView) findViewById(R.id.tvJsonItem);    btnHit.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            new JsonTask().execute("http://data.nba.net/10s/prod/v1/2016/players.json");        }    });}private class JsonTask extends AsyncTask<String, String, String> {    protected void onPreExecute() {        super.onPreExecute();        pd = new ProgressDialog(MainActivity.this);        pd.setMessage("Please wait");        pd.setCancelable(false);        pd.show();    }    protected String doInBackground(String... params) {        HttpURLConnection connection = null;        BufferedReader reader = null;        try {            URL url = new URL(params[0]);            connection = (HttpURLConnection) url.openConnection();            connection.connect();            InputStream stream = connection.getInputStream();            reader = new BufferedReader(new InputStreamReader(stream));            StringBuffer buffer = new StringBuffer();            String line = "";            while ((line = reader.readLine()) != null) {                buffer.append(line+"\n");                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)            }我的問題是我無法從我選擇的來源獲得單個標簽。例如,我如何獲取該流中每個玩家的名字和姓氏并記錄下來?感謝您的時間和考慮。
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

首先,我建議您使用任何用于 android 的 http 庫(okHttp,volley ..)


但是如果你仍然想使用你實現它的方式,你需要在這里做一些改變:


while ((line = reader.readLine()) != null) {

            buffer.append(line);

            Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)


        }


        String json = buffer.toString();

try {

  String json = "";

  JSONObject jsonObject = new JSONObject(json);

  JSONObject league = jsonObject.getJSONObject("league");

  JSONArray standard = league.getJSONArray("standard");

  for (int i = 0;i<standard.length();i++){

    JSONObject item = standard.getJSONObject(i);

    String name = item.getString("firstName");

    String lastName= item.getString("lastName");

  }

} catch (JSONException e) {

  e.printStackTrace();

}


查看完整回答
反對 回復 2022-05-25
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號