您是否閱讀過此JSON,我對JSON仍然不熟悉以獲取數據。獲取數據JSON Android錯誤
這裏是JSON:
{
"cliente": [
{
"cpf": "82334673382",
"endereco": "ewkiewowieou",
"id": "11",
"nome": "Wagner Assis"
},
{
"cpf": "82334889929",
"endereco": "uuqwuwqu",
"id": "14",
"nome": "GS Advogados"
},
{
"cpf": "3237287383",
"endereco": "ewiueiwu",
"id": "15",
"nome": "Empreiteira GH"
},
{
"cpf": "73448723349",
"endereco": "dsgdsjhdjh",
"id": "17",
"nome": "Cobrança HH"
},
{
"cpf": "7337474847",
"endereco": "weuwuewiu",
"id": "18",
"nome": "Pollo GH"
},
{
"cpf": "23423423",
"endereco": "rewrwer",
"id": "19",
"nome": "Finanças LH"
},
{
"cpf": "847384378",
"endereco": "jsjsdhjsdh",
"id": "20",
"nome": "Empreisa SA"
},
{
"cpf": "123456",
"endereco": "ewewew",
"id": "21",
"nome": "João Pedro"
},
{
"cpf": "73447832828",
"endereco": "dsjdhsjh",
"id": "22",
"nome": "Pedro Otavio"
},
{
"cpf": "312312",
"endereco": "rwwree",
"id": "23",
"nome": "Carlos Philip"
}
]
}
這裏就是我拉的數據的方法:
public void handleResponse(String response) {
EditText edFirstName = (EditText) findViewById(R.id.testeNome);
EditText edLastName = (EditText) findViewById(R.id.testeEnder);
EditText edEmail = (EditText) findViewById(R.id.testeCpf);
edFirstName.setText("");
edLastName.setText("");
edEmail.setText("");
try {
//------------------------------//
//Array Json
JSONArray JArray = new JSONArray(response.toString());
JSONObject JObjeto = JArray.getJSONObject(0);
JSONObject posicao = JObjeto.getJSONObject(KEY_CLI);
String firstName = posicao.getString("id");
edFirstName.setText(firstName);
}
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
錯誤說這是不可能的轉換對象數組。有人能幫我嗎?
您的'response'字符串包含您向我們展示的對象,這意味着您無法從中創建'JSONArray'。你應該從'response'創建一個對象,獲得由'cliente'鍵索引的數組,然後處理這個列表中的每個條目。 – bvidal
所以我必須處理我的JSON對象,而不是一個數組?會給我一個例子嗎?是我是json的新手,只能做到只有一個客戶,而不是一個數字.. – zullyB
看看下面的答案,這將是一個好開始 – bvidal