C
C
CeBePHblY2016-06-08 13:00:11
Java
CeBePHblY, 2016-06-08 13:00:11

Android (Java) - work with JSON array. How to get all elements one by one?

the following json comes from the server:

{
"command":"client_history_requests",
"client_requests": 
[
{"client_id_request":"10", "client_request_year":"2016", "client_request_mounth":"06", "client_request_day":"07", "client_request_hour":"19", "client_request_minute":"12", "client_request_second":"42"},
{"client_id_request":"11", "client_request_year":"2016", "client_request_mounth":"06", "client_request_day":"07", "client_request_hour":"19", "client_request_minute":"35", "client_request_second":"49"},
{"client_id_request":"12", "client_request_year":"2016", "client_request_mounth":"06", "client_request_day":"07", "client_request_hour":"19", "client_request_minute":"37", "client_request_second":"53"},
{"client_id_request":"13", "client_request_year":"2016", "client_request_mounth":"06", "client_request_day":"07", "client_request_hour":"19", "client_request_minute":"40", "client_request_second":"00"},
]
}

those. client_requests contains an array. In android I do this:
JSONObject jsonFromServer = new JSONObject(dataFromServer);
                    String command = jsonFromServer.getString("command");
                    if (command.equals(SERVER_COMMAND_CLIENT_HISTORY_REQUESTS)){
                        JSONArray arrayClentHistoryRequest = jsonFromServer.getJSONArray("client_requests");
                        JSONObject jsonArrayRequest= new JSONObject(arrayClentHistoryRequest.getString(1));
                        Toast.makeText(AddNewRequestActivity.this, jsonArrayRequest.getString("client_request_year"), Toast.LENGTH_LONG).show();

and 2016 will turn out to me in a toast, i.e. the client_request_year value of the first element. and how to get client_request_year from the second third, etc.?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2016-06-08
@CeBePHblY

Обернуть последние две строки в for:

for (int i=0; i<arrayClentHistoryRequest .length();i++)
{
Toast.makeText(AddNewRequestActivity.this, arrayClentHistoryRequest .get(i).getString("client_request_year"), Toast.LENGTH_LONG).show();
}

В предпоследней строке у вас вообще непонятно что и зачем делается.

A
ape364, 2016-06-08
@ape364

Стоит также взглянуть на Retrofit. Тут вот какой-то мануал на русском.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question