杰森(Json)没出现

时间:2019-05-29 09:39:30

标签: java android firebase

我试图从json捕获值,但没有arrays(name)。所以我需要直接能够数据。我收到有关“ getJSONObject(0)”的警告,它说“ String.valueOf()”,所以我将其应用于此。

我的JSON link available here

{
  "-LZEGO2dtHMm7zNP05Jd" : {
    "NewZaman" : "20.02.2019 / 22:45",
    "Oynayanlar" : "Derby - Millwall",
    "Tahmin" : "UNDER 2.5",
    "Tahmin2" : "ALT 1.5",
    "id" : "85",
    "mackelesonucu" : 44,
    "sonuckelesen" : "Win",
    "sonuckelestr" : "Kazandı",
    "sortingId" : -85
  },
  "-LZEJ_ohgg3eLr1Jdv38" : {
    "NewZaman" : "20.02.2019 / 13:15",
    "Oynayanlar" : "Dunav Ruse - FC Vereya",
    "Tahmin" : "1",
    "Tahmin2" : "1",
    "id" : "86",
    "mackelesonucu" : 44,
    "sonuckelesen" : "Win",
    "sonuckelestr" : "Kazandı",
    "sortingId" : -86
   }
}

代码

解析代码

    private void parseJSON() {
        String url ="http://xjamal.epizy.com/tahminall-dene.json";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            //JSONArray jsonArray = response.getJSONArray("hits");
                            JSONObject jsonArray = response.getJSONObject(String.valueOf(0));

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject hit = jsonArray.getJSONObject(String.valueOf(i));


                                String creatorName = hit.getString("sonuckelesen");
                                String likeCount = hit.getString("mackelesonucu");

                                mExampleList.add(new arsivlist(creatorName, likeCount));
                            }

                            mExampleAdapter = new ArsivAdapter(Arsivgoster.this, mExampleList);
                            mRecyclerView.setAdapter(mExampleAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }
}

现在不显示json。页面显示为空。

更新库兰的查询结果。

    private void parseJSON() {
        String url ="http://xjamal.epizy.com/tahminall-dene.json";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            Iterator<String> keys = response.keys();

                            while(keys.hasNext()) {
                                String key = keys.next();
                                if (response.get(key) instanceof JSONObject) {
                                    JSONObject innerObject = response.getJSONObject(key);
                                    String creatorName = innerObject.getString("sonuckelesen");
                                    String likeCount = innerObject.getString("mackelesonucu");

                                    mExampleList.add(new arsivlist(creatorName, likeCount));

                                }
                            }
                            mExampleAdapter = new ArsivAdapter(Arsivgoster.this, mExampleList);
                            mRecyclerView.setAdapter(mExampleAdapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试像这样解析您的响应json对象

Iterator<String> keys = response.keys();

        while(keys.hasNext()) {
            String key = keys.next();
            if (response.get(key) instanceof JSONObject) {
                JSONObject innerObject = response.getJSONObject(key);
                String creatorName = innerObject.getString("sonuckelesen");
                String likeCount = innerObject.getString("mackelesonucu");

                mExampleList.add(new arsivlist(creatorName, likeCount));

            }
        }
        mExampleAdapter = new ArsivAdapter(Arsivgoster.this, mExampleList);
        mRecyclerView.setAdapter(mExampleAdapter);

将此代码替换为您在try块中的代码