【问题标题】:How to get the string of a json child in a json array如何在json数组中获取json子的字符串
【发布时间】:2020-05-08 02:15:10
【问题描述】:

我想在 JSON 文件中获取 JSON 字符串的值,该字符串嵌套在 JSON 数组中我正在使用 android studio 这是我的 JSON 文件

{
  "Amber":
    {
      "Info": {
        "name": "Amber",
        "bio": "Turkish-Cypriot",
        "image": "null"
      },
      "Class": {
        "Adorable": {
          "name": "",
          "link": ""
        },
        "IronAge": {
          "name": "",
          "link": ""
        }
      }
    }
}

我想将 name 和 bio 的值变成一个字符串,这是我尝试过的 java 代码

    @SuppressLint("StaticFieldLeak")
    public class AsyncHttpTask_GetWebData extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpsURLConnection urlConnection = null;
            try {
                url = new URL(urls[0]);
                urlConnection = (HttpsURLConnection) url.openConnection();
                if (result != null) {
                    String response = streamToString(urlConnection.getInputStream());
                    parseResult_GetWebData(response);
                    return result;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                Toast.makeText(MainActivity.this, "Data Loaded Successfully", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Failed to Data!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private String streamToString(InputStream stream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
        String line;
        String result = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }
        if (null != stream) {
            stream.close();
        }
        return result;
    }

    private void parseResult_GetWebData(String result) {
        try {
            JSONObject response = new JSONObject(result);
            JSONArray jsonArray = response.getJSONArray("Amber");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject =jsonArray.optJSONObject(i);
                JSONObject jsonObject1 = jsonObject.getJSONObject("Info");
                String name = jsonObject1.optString("name");
                textView.setText(name);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

textview 是空的并且没有显示结果,但是我得到 OnPostExecute toast 说数据已成功加载但值未显示在 textview 中

【问题讨论】:

  • 根据错误信息,我认为这与JSON或您的主题无关。
  • 但我已经使用相同的代码块来解析其他 JSON 数据
  • 我已经编辑了问题,但现在没有得到任何结果,即 textview 没有显示任何数据

标签: java android arrays json android-studio


【解决方案1】:

首先,我建议你使用Gson库来转换Json

其次,JsonArray 的格式如下:

"arrary_key": [
            {
                "name": "Jeff Bridges",
                "id": "162655890",
                "characters": [
                    "Jack Prescott"
                ]
            },
            {
                "name": "Charles Grodin",
                "id": "162662571",

            }
]

所以第二个它仍然是JsonObject

JSONObject response = new JSONObject(result);
JSONObject jsonObject = response. getJSONObject("Amber");

或者你可以要求服务返回 jsonArray

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多