-1

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!

emt1803
  • 35
  • 7

4 Answers4

0

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

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

Rudresh
  • 731
  • 1
  • 10
  • 26
0

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.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Vishu Bhardwaj
  • 368
  • 1
  • 9
  • Thank you, but the compiler says at the line: .getJSONArray("Players"); : "Cannot resolve method....." – emt1803 Dec 30 '16 at 08:10
0

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.. :)

0

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);
Muntaser Ahmed
  • 4,487
  • 1
  • 16
  • 17
  • Instead of using adapter.notifyDataSetChanged(); did you try setting up the adapter the way I have it? Are you getting any errors? If so, please post the stack trace. – Muntaser Ahmed Dec 30 '16 at 08:39
  • I tried it with your Listview Adapter. There is no change, but I found the error but I do not know how to solve it. The response from volley is completly empty. – emt1803 Dec 30 '16 at 08:48
  • Are you sure players is being populated? Can you log the length/contents of the list to check? Furthermore, you should be putting a log statement in the catch branch in case an exception is being thrown. – Muntaser Ahmed Dec 30 '16 at 08:57