解析JSONObjects并转换为Java对象时遇到奇怪的问题

时间:2017-08-23 16:36:24

标签: java android json

这是我第一次遇到一个非常奇怪的问题。我有一个下面提到的JSONObject。:

JSON正文链接(由于字符限制,此处无法粘贴JSON正文): http://kolam.vicz.in:7890/games_gifs/

我正在解析上面的JSONObjects集并将它们转换为Java对象。下面是我解析这个JSON主体的代码。

private void getGameList() {
        StringRequest request = new StringRequest(Request.Method.GET, gameUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject mainObject = new JSONObject(response);
                    JSONArray gamesArray = mainObject.getJSONArray("TracingGames");
                    Log.e(TAG, "gameArray length:" + gamesArray.length());
                    for (int i = 0; i < gamesArray.length(); i++) {
                        JSONObject obj = gamesArray.getJSONObject(i);
                        for (String str : getKeys(obj)) {
                            Log.e(TAG, str);
                            if (str.equalsIgnoreCase("kolam")) {

                                /*Section for Learn and Kolam Tracing games start*/
                                //TODO: Need to add the game names to the object (Need server side implementation as well)
                                KolamTracingGames kolamTracingGames = new KolamTracingGames();
                                kolamTracingGames.setKolamGifPath(obj.getString("path"));
                                kolamTracingGames.setKolamLevel(Integer.parseInt(str));
                                kolamTracingGames.setKolamGameName("Kolam Tracing");
                                kolamTracingGames.setX(getXCoordinates(obj));
                                kolamTracingGames.setY(getYCoordinates(obj));
                                kolamObjects.add(kolamTracingGames);
                                break;
                            } else if (str.equalsIgnoreCase("level")) {
                                LearnTracingGames learnTracingGames = new LearnTracingGames();
                                learnTracingGames.setLearnGameGifPath(obj.getString("path"));
                                learnTracingGames.setLearnGameLevel(Integer.parseInt(str));
                                learnTracingGames.setGameName("Learn Game");
                                learnTracingGames.setX(getXCoordinates(obj));
                                learnTracingGames.setY(getYCoordinates(obj));
                                learnGameObjects.add(learnTracingGames);
                                Log.e(TAG, learnGameObjects.size() + "");
                                break;
                            }
                        }
                    }

                    if (gameType.equalsIgnoreCase("Trace the Kolam")) {
                        kolamTraceAdapter = new KolamTraceAdapter(getActivity());
                        kolamTraceAdapter.getGameList(kolamObjects);
                        recyclerView.setAdapter(kolamTraceAdapter);
                    } else if (gameType.equalsIgnoreCase("Learn")) {
                        learnGameAdapter = new LearningGameAdapter(getActivity());
                        learnGameAdapter.getGameList(learnGameObjects);
                        Log.e(TAG, "learngameobject size:" + learnGameObjects.size());
                        recyclerView.setAdapter(learnGameAdapter);
                        Log.e(TAG, "Learn games");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.getMessage());
                if (getActivity() != null) {
                    Alerter.create(getActivity())
                            .setTitle(R.string.warning)
                            .setText(R.string.network_error)
                            .setDuration(2000)
                            .setBackgroundColorRes(R.color.dot_dark_screen1)
                            .show();
                }
            }
        });
        request.setTag(TAG);
        request.setRetryPolicy(new DefaultRetryPolicy(30000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(request);

我的问题是有8个级别类型对象和4个 kolam 对象(为了清晰起见,请参阅JSON正文)。我有两个单独的POJO课程。我能够正确地解析JSON主体,但是当我尝试创建那些JSONObjects的Java对象时,最内部的for loop没有完全运行。它在1循环后停止。

如果我这样做(创建Java对象),内部for循环没有完全执行(只执行一次但应该执行超过12次):

for (int i = 0; i < gamesArray.length(); i++) {
                        JSONObject obj = gamesArray.getJSONObject(i);
                        for (String str : getKeys(obj)) {
                            Log.e(TAG, str);
                            if (str.equalsIgnoreCase("kolam")) {

                                /*Section for Learn and Kolam Tracing games start*/
                                //TODO: Need to add the game names to the object (Need server side implementation as well)
                                KolamTracingGames kolamTracingGames = new KolamTracingGames();
                                kolamTracingGames.setKolamGifPath(obj.getString("path"));
                                kolamTracingGames.setKolamLevel(Integer.parseInt(str));
                                kolamTracingGames.setKolamGameName("Kolam Tracing");
                                kolamTracingGames.setX(getXCoordinates(obj));
                                kolamTracingGames.setY(getYCoordinates(obj));
                                kolamObjects.add(kolamTracingGames);
                                break;
                            } else if (str.equalsIgnoreCase("level")) {
                                LearnTracingGames learnTracingGames = new LearnTracingGames();
                                learnTracingGames.setLearnGameGifPath(obj.getString("path"));
                                learnTracingGames.setLearnGameLevel(Integer.parseInt(str));
                                learnTracingGames.setGameName("Learn Game");
                                learnTracingGames.setX(getXCoordinates(obj));
                                learnTracingGames.setY(getYCoordinates(obj));
                                learnGameObjects.add(learnTracingGames);
                                Log.e(TAG, learnGameObjects.size() + "");
                                break;
                            }
                        }
                    }

getKeys(JSONObject)方法如下:

private static String[] getKeys(JSONObject firstJSONObject) {
        Iterator keysToCopyIterator = firstJSONObject.keys();
        List<String> keysList = new ArrayList<>();
        while (keysToCopyIterator.hasNext()) {
            String key = (String) keysToCopyIterator.next();
            keysList.add(key);
        }
        return keysList.toArray(new String[keysList.size()]);
    }

但如果我避免在for loop内创建对象,则内部for loop会完全运行。

1 个答案:

答案 0 :(得分:2)

  

但是如果我避免在for循环中创建对象那么内部为   循环完全运行。

Integer.parseInt("level")Integer.parseInt("kolam")显然是崩溃

解释

当您if (str.equalsIgnoreCase("kolam"))else if (str.equalsIgnoreCase("level")) {中的任何一个匹配时

你明显发生了崩溃Integer.parseInt(str),因为strlevelkolam显然不是integers

解决方案:请勿使用str取代值

if (str.equalsIgnoreCase("kolam")) {
    //... code
    kolamTracingGames.setKolamLevel(obj.optInt(str));
    //... code                      ^^^^^^^^^^^^^^^
    break;
} else if (str.equalsIgnoreCase("level")) {
    //... code
    learnTracingGames.setLearnGameLevel(obj.optInt(str));
    //... code                          ^^^^^^^^^^^^^^^
    break;
}