0

I have the following Json Format file:

[
    {
        "id": 1,
        "game_genre": "RPG"
     },
     {
        "id": 2,
        "game_genre": "Action"
     }
] 

on a remote location: http://127.0.0.1:8000/genre/?format=json (Ddjango Format) and i want to populate an Android spinner with the respective id so that can perform a search later depending on the id of the genre. I read the other threads but since i am new into android i don't know where to start. i have the Json parser file included in my project

dirkgroten
  • 20,112
  • 2
  • 29
  • 42

1 Answers1

1

Here is the code for JSON parsing :

String json = "[ { id: 1, game_genre: RPG }, { id: 2, game_genre : Action }]";


    if (json != null) {
        try {
                ArrayList<String> game = new ArrayList();
            ArrayList<Integer> id = new ArrayList();
                JSONArray jsonArray = new JSONArray(json);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    game.add(jsonObject1.getString("game_genre"));
                    id.add(Integer.parseInt(jsonObject1.getString("id")));
                }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(Activity.this, "Cannot get json from server", Toast.LENGTH_SHORT).show();
    }

Then you just need to pass the game ArrayList to the Spinner adapter. How can I add items to a spinner in Android?

bhumilvyas
  • 121
  • 5