如何获取json数组的子节点的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取json数组的子节点的值相关的知识,希望对你有一定的参考价值。

[我在解析此链接(https://gnews.io/api/v3/top-news?&token=dd21eb88599ccb3411eaad9b314cde23)的数据列表时遇到一些问题,我能够从json数组(文章)中获取数据,但是我如何从josn数组中获取数据(源)

    private void getWebApiData() {
        String WebDataUrl = "https://gnews.io/api/v3/top-news?&token=dd21eb88599ccb3411eaad9b314cde23";
        new AsyncHttpTask.execute(WebDataUrl);
    }

    @SuppressLint("StaticFieldLeak")
    public class AsyncHttpTask 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(response);
                    return result;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                newsAdapter = new NewsAdapter(getActivity(), newsClassList);
                listView.setAdapter(newsAdapter);
                Toast.makeText(getContext(), "Data Loaded Successfully", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getContext(), "Failed to load data!", Toast.LENGTH_SHORT).show();
            }
            progressBar.setVisibility(View.GONE);
        }
    }

    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;
        }

        // Close stream
        if (null != stream) {
            stream.close();
        }
        return result;
    }


    private void parseResult(String result) {
        try {
            JSONObject response = new JSONObject(result);
            JSONObject response2 = response.getJSONObject("articles");
            NewsClass newsClass;
            for (int i = 0; i < newsClass.length(); i++) {
                JSONObject post = newsClass.optJSONObject(i);
                String title = post.optString("title");
                newsClass = new newsClass();
                newsClass.setNews_Title(title);
                artistClassList.add(newsClass);
            }

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

这是我正在使用的代码,用于获取文章的数据。获取我尝试过的资源

    private void parseResult(String result) {
        try {
            JSONObject response = new JSONObject(result);
            JSONObject response2 = response.getJSONArray("articles");
            JSONObject response3 = response2.getJSONObject("sources");
            NewsClass newsClass;
            for (int i = 0; i < newsClass.length(); i++) {
                JSONObject post = newsClass.optJSONObject(i);
                String title = post.optString("title");
                newsClass = new newsClass();
                newsClass.setNews_Title(title);
                artistClassList.add(newsClass);
            }

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

但是我想我没有正确获得代码

这是我尝试的第二个选项

    private void parseResult(String result) {
        try {
            JSONObject response = new JSONObject(result);

            JSONObject response = response2.getJSONObject("sources");
            NewsClass newsClass;
            for (int i = 0; i < newsClass.length(); i++) {
                JSONObject post = newsClass.optJSONObject(i);
                String title = post.optString("title");
                newsClass = new newsClass();
                newsClass.setNews_Title(title);
                artistClassList.add(newsClass);
            }

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

但是这只给我空文本字段,用于填充数据的空格,但为空白请任何帮助将不胜感激

答案

我不知道您的代码如何工作。您已尝试将JSONObject用作articles >>,实际上是JSONArray。除此之外,我没有在您的json中找到任何key,例如sources,而是找到了[[source。要解析source,请尝试以下方式:try { JSONObject jsonObject = new JSONObject(result); JSONArray jsonArray = jsonObject.getJSONArray("articles"); for(int i = 0; i < jsonArray.length(); i++) { JSONObject articleObject = jsonArray.getJSONObject(i); JSONObject sourceObject = articleObject.getJSONObject("source"); String name = sourceObject.optString("name"); String url = sourceObject.optString("url"); } } catch (JSONException e) { e.printStackTrace(); }

以上是关于如何获取json数组的子节点的值的主要内容,如果未能解决你的问题,请参考以下文章

Jmeter 解析json文件(多条件获取数组中某个节点的值)

根据特定的子值获取 json 值

java中把json怎么转换成数组

java中把json怎么转换成数组?

如何获取json数组中子数组的值

Objective c- 获取 json 数组中的子 json 字符串