我如何在android中访问JSON数据?

为了加入以下我的JSON数据:

[{"id":"1","code":"2020-04-05-20:12:12","cases":"313","recovered":"34","deaths":"6","datesituation":"2020-04-08","timesituation":"16:35:00"}]

我使用下面的代码:

private void getGhanaSituation() {

        dataBaseHelper.deleteGhana();
        JsonArrayRequest req = new JsonArrayRequest("MyURL",
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        try {

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

                                JSONObject ghsituation = (JSONObject) response.get(i);

                                int cases = ghsituation.getInt("cases");
                                int recovered = ghsituation.getInt("recovered");
                                int deaths = ghsituation.getInt("deaths");
                                String datesituation = ghsituation.getString("datesituation");
                                String timesituation = ghsituation.getString("timesituation");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                        hidepDialog();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("TAG", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), "Error"+error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();
            }
        });

        VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(req);
    }

现在我使用了一个API,它给我下面的其他格式:

{"ghana":{"existing":"273","confirmed":"313","recovered":"34","deaths":"6","date":"08th April, 2020","time":"16:35"},"global":{"existing":"1,066,963","confirmed":"1,468,891","recovered":"316,482","deaths":"85,446","date":"08th April, 2020","time":"16:35"}}

我更改了代码:

private void getGhanaSituation() {

        dataBaseHelper.deleteGhana();
        JsonArrayRequest req = new JsonArrayRequest("https://mazitekgh.com/covid19/v1/",
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        try {

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

                                JSONObject ghsituation = (JSONObject) response.get(i);

                                int cases = ghsituation.getInt("confirmed");
                                int recovered = ghsituation.getInt("recovered");
                                int deaths = ghsituation.getInt("deaths");
                                String datesituation = ghsituation.getString("date");
                                String timesituation = ghsituation.getString("time");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                        hidepDialog();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("TAG", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), "Error"+error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();
            }
        });

        VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(req);
    }

当运行APP时,我收到错误消息:*** Errororg.json.JSONException:值:{“ ghana”:{“ existing”:“ 273”,“ confirmed”:“ 313”,“ recovered”:“ 34” ,“死亡”:“ 6”,“日期”:“ 2020年4月8日”,“时间”:“ 16:35”},“全局”:{“现有”:“ 1,066,963”,“已确认”:“ 1,468,891” “,”已恢复“:” 316,482“,”死亡“:” 85,446“,”日期“:” 2020年4月8日“,”时间“:” 16:35“}}无法转换为JSONArray。

拜托我需要你的帮忙。

评论
  • ncum
    ncum 回复

    I would advise you to use a parser for converting JSON to Java Object as it'll make your life easier and you'll be able to manipulate the data more freely.

    你有很多选择

    • Moshi is now in trend, you can use it quite easliy, here is a gist that shows how to use this library.

    • Gson, here is also a quick tutorial

    • Jackson, another tutorial to help you get the hand of it.

    他们都有自己的优点和缺点。看看githubs并选择一个:)