How to retrieve whole JSON object with key value?

MowKette :

It certainly has been already asked but I wasn't able to search the solution for my case.

I have two JSON file : quizzes.json and quizzesQuestions.json. The first one only has the titles and id of every quizzes and the second one has all the datas needed for each quiz.

quizzes.json:

 [
      {
        "_id": "1",
        "title": "blabla"
      },
      {
        "_id": "2",
        "title": "blabla2"
      },
      {
        "_id": "3",
        "title": "blabla3"
      },
      {
        "_id": "4",
        "title": "blabla4"
      }
 ]

and quizzesQuestions.json:

[
    {
        "quizId": "1",
        "questions": [
            {
                "questionId": "1",
                "translations": [
                    {
                        "lang": "fr",
                        "label": "Quelle est la reponse ?",
                        "answers": [
                            "reponse 1",
                            "reponse 2",
                            "reponse 3",
                            "reponse 4"
                        ],
                        "trueAnswer": "reponse 1 2 3 or 4",
                        "explanation": "parce que..."
                    },
                    {
                        "lang": "en",
                        "label": "What is the answer ?",
                        "answers": [
                            "answer 1",
                            "answer 2",
                            "answer 3",
                            "answer 4"
                        ],
                        "trueAnswer": "answer 1 2 3 or 4",
                        "explanation": "because..."
                    },
                    {
                        "lang": "de",
                        "label": "What is the answer ?",
                        "answers": [
                            "answer 1",
                            "answer 2",
                            "answer 3",
                            "answer 4"
                        ],
                        "trueAnswer": "answer 1 2 3 or 4",
                        "explanation": "because..."
                    },
                    {
                        "lang": "sp",
                        "label": "What is the answer ?",
                        "answers": [
                            "answer 1",
                            "answer 2",
                            "answer 3",
                            "answer 4"
                        ],
                        "trueAnswer": "answer 1 2 3 or 4",
                        "explanation": "because..."
                    }
                ]
            },
            {
                "questionId": "2",
                "translations": [
                    {
                        "lang": "fr",
                        "label": "Quelle est la reponse ?",
                        "answers": [
                            "reponse 1",
                            "reponse 2",
                            "reponse 3",
                            "reponse 4"
                        ],
                        "trueAnswer": "reponse 1 2 3 or 4",
                        "explanation": "parce que..."
                    },
                    {
                        "lang": "en",
                        "label": "What is the answer ?",
                        "answers": [
                            "answer 1",
                            "answer 2",
                            "answer 3",
                            "answer 4"
                        ],
                        "trueAnswer": "answer 1 2 3 or 4",
                        "explanation": "because..."
                    },
                    {
                        "lang": "de",
                        "label": "What is the answer ?",
                        "answers": [
                            "answer 1",
                            "answer 2",
                            "answer 3",
                            "answer 4"
                        ],
                        "trueAnswer": "answer 1 2 3 or 4",
                        "explanation": "because..."
                    },
                    {
                        "lang": "sp",
                        "label": "What is the answer ?",
                        "answers": [
                            "answer 1",
                            "answer 2",
                            "answer 3",
                            "answer 4"
                        ],
                        "trueAnswer": "answer 1 2 3 or 4",
                        "explanation": "because..."
                    }
                ]
            }
        ]
    }
]

I need to retrieve one object from a JSON file (for now, it will become a database in the future) which is equal to the a specific id. Here there is only one quiz with an id of 1.

I'm already able to retrieve every quizzes from the first file with Gson and display them in a list. Now I want to be able to retrieve only the quiz which has the same id as the one the user clicked on but I don't know how to proceed.

Here is my Gson code if it helps you understand what I want to do:

private void parseJSON() {

    String quizzesString = loadJSONFromAsset();

    try {
        JSONArray array = new JSONArray(quizzesString);
        if(array.length() > 0) {
            Gson gson = new Gson();
            int i = 0;
            while(i < array.length()) {
                quizzes.add(gson.fromJson(array.getJSONObject(i).toString(), Quizzes.class));
                i++;
            }
        } else {
            Log.d(TAG, "parseJSON: No Objects");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

private String loadJSONFromAsset() {
    String json;
    try {
        InputStream is = getApplication().getApplicationContext().getAssets().open("quizzes.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}
Michał Ziober :

You can deserialise JSON directly from InputStream. Also, you do not need to use org.json to read JSON. Try to directly parse data using Gson:

private Map<String, Quizzes> loadQuizzes(InputStream jsonInputStream) throws IOException {
    Gson gson = new GsonBuilder().create();
    Type quizzesType = new TypeToken<List<Quizzes>>() {}.getType();

    try (InputStreamReader reader = new InputStreamReader(jsonInputStream, StandardCharsets.UTF_8)) {
        List<Quizzes> quizzes = gson.fromJson(reader, quizzesType);

        return quizzes
                .stream()
                .collect(Collectors.toMap(Quizzes::getQuizId, q -> q));
    }
}

Now, you can lookup Quizzes object from Map by id.

See also:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158320&siteId=1