Então, tenho a seguinte string JSON que vem de uma API externa:
{
"status":"ok",
"clientes":[
{
"cliente":
"{
\"id\":\"1\",
\"nome\":\"Carlos\"
}"
},
{
"cliente":
"{
\"id\":\"8\",
\"nome\":\"Cleonice Rocha\"
}"
}
]
}
Estou transformando ela em objeto e pegando o atributo “equipamentos” como abaixo:
JSONObject retorno = null;
try {
retorno = new JSONObject(str);
JSONArray arrClientes = retorno.getJSONArray("clientes");
preencheComboClientes(arrClientes);
}
E enviando esse array para o método preencheComboClientes
Agora a minha necessidade é de pegar desse array, os objetos {} que existem dentro dele e de cada objeto, pegar os atributos id e nome de preencher uma ArrayList para popular um Spinner
Estou tentando como abaixo mas não está funcionando.
private void preencheComboClientes(JSONArray jsClientes) throws JSONException {
ArrayList<JSONObject> arrayListClientes = new ArrayList<JSONObject>();
for (int i = 0; i < jsClientes.length(); i++) {
JSONObject cliente = new JSONObject();
cliente.put("id", jsClientes.getJSONObject("cliente").getInt("id"));
cliente.put("nome", jsClientes.getJSONObject("cliente").getString("nome"));
arrayListClientes.add(cliente);
}
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayListClientes);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCliente.setAdapter(spinnerArrayAdapter);
}
Tem alguma coisa errada e não estou conseguindo descobrir como se faz.