-1

Now, I want to create Gridview with section and I do following this

and then I stuck problem at I don't know how to get position section and item from JSONArray

I have JSONArray like this

“Team”: [
        {
          "team_id": "16",
          "team_name": "3",
          "team_max_player": "6",
          "team_amount": "2",
          "team_member_list": [
            {
              "id": "19",
              "room_id": "23",
              "user_id": "75",
              "team_id": "16",
              "detail_status": ""
            },
            {
              "id": "21",
              "room_id": "23",
              "user_id": "46",
              "team_id": "16",
              "detail_status": ""
            }
          ]
        },
        {
          "team_id": "14",
          "team_name": "1",
          "team_max_player": "1",
          "team_amount": "1",
          "team_member_list": [
            {
              "id": "20",
              "room_id": "23",
              "user_id": "40",
              "team_id": "14",
              "detail_status": ""
            }
          ]
        }
      ]

I want to set "team_id" as section and "team_member_list" as an item in that section. please teach me to do it.

I am currently in grade 12 in school with programing class, android studio, JAVA Moblie App. Please help me and sorry about my English.

Aloil
  • 9
  • 5
  • Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Ryan M Jun 05 '20 at 08:19
  • It's also unclear what you mean by "don't know how to get position section and item from JSONArray" - there's nothing about positions in your JSON data – Ryan M Jun 08 '20 at 22:29

1 Answers1

1

First you can load the JSON as a string and parse the JSON string using the JSONObject class.

You can then use the JSONArray class to parse the JSON Array inside your JSONObject to retrieve the details you want.

See the code sample below that will log each team member's id along with the id of which team they belong to in the console.

    JSONObject jsonObject = new JSONObject(jsonString);
    JSONArray teams = jsonObject.getJSONArray("Team");
    for (int i = 0; i < teams.length(); i++) {
        String teamId = jsonObject.getJSONArray("Team").getJSONObject(i).getString("team_id");
        JSONArray teamMemberList = jsonObject.getJSONArray("Team").getJSONObject(i).getJSONArray("team_member_list");
        for (int j = 0; j < teamMemberList.length(); j++) {
            String teamMemberId = teamMemberList.getJSONObject(j).getString("id");
            Log.i("team", "team id: " + teamId + " team member id: " + teamMemberId);
        }
    }

If you would like to learn more about JSON parsing in Android, I wrote a JSON tutorial for Android that goes into much greater detail.

  • Thank you, but I want to get the position of "team_id" to set Section in method "sections.add(new SectionedGridRecyclerViewAdapter.Section( "position","team_name"));" to create Gridview with section – Aloil Jun 04 '20 at 07:33
  • Can you share the code relating to Section in your question? – Learn to Droid Jun 04 '20 at 07:46
  • I post my code below comment please help me to do it. I stuck on this page in my app too many days – Aloil Jun 04 '20 at 10:33