Android JSON Array Parsing

117 Views Asked by At

How can I parse this Json API with a webrequest, exactly the Element "Players":

    {
    "data": [
        {
            "Id": *,
            "ModId": *,
            "appId": ***********,
            "online": *,
            "Servername": "**** **** *****",
            "IpAddress": "**.**.**.**",
            "Port": ****,
            "ServerPassword": ********,
            "StartParameters": "*******",
            "Slots": **,
            "Playercount": **,
            "Civilians": **,
            "Medics": *,
            "Cops": *,
            "Adac": *,
            "Players": [
                "***********",
                "***********",
                "******",
                "******"
            ]
        }
    ]
}

I want it to parse that I get on every time when the loop is running one playername to add it to a listview. But I do not know how to parse the "Players" element like this.

Thank you for your help!

4

There are 4 best solutions below

3
On BEST ANSWER

The following code populates a list with the players from your JSON string.

List<String> players = new ArrayList<>();

try {
    JSONObject jsonObj = new JSONObject(jsonString);
    JSONArray data = jsonObj.getJSONArray("data");
    JSONObject dataObj = data.getJSONObject(0);
    JSONArray playersJson = dataObj.getJSONArray("Players");

    for (int i = 0; i < playersJson.length(); i++) {
        players.add(playersJson.get(i).toString());
    }
} catch (JSONException e) {
    Log.e(TAG, e.getMessage());
}

You can then use this list along with an ArrayAdapter to populate your ListView.

    ArrayAdapter<String> adapter;
    ListView listView = (ListView) findViewById(R.id.listView);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, players);
    listView.setAdapter(adapter);
0
On

Use new concept Retofit here you no need to parse just creating pojo class,Refer below link

https://square.github.io/retrofit/

1
On

you can save an index of how many elements have been removed from the JSON. like

int index = 0;

and create a method that have access to JSON and pass index to it like

public String getPlayerFromJsonAtIndex(int i) {...}.

Now access the JSON string and parse it using JSONObject (hint store the parsed Players array at once in starting). Let you parsed Players array like

JSONArray players = (new JSONObject(jsonString))
  .getJSONArray("data")
  .get(0)
  .getJSONArray("Players");

Now from this players JSONArray get string at index provided. like

{
  return (String)players.get(i);
}

Hope this helps thank you.

0
On

Use this below code to parse reponse.

JSONObject objMain = new JSONObject(yourData);
        try {
            JSONArray respArray = objMain.getJSONArray("data");
            for(int i=0;i<respArray.length();i++)
            {
                JSONObject dataObj = respArray.getJSONObject(i);
                String id = dataObj.getString("id");
                -
                        -
                JSONArray arrayPlayer = dataObj.getJSONArray("players");
                for(int j=0;j<arrayPlayer.length();j++){
                    JSONObject playerObj = arrayPlayer.getJSONObject(j);
                    ---
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

and to take remaining data use proper parameters.. :)