2014-01-19 54 views
-4

您是否閱讀過此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); 
    } 

} 

錯誤說這是不可能的轉換對象數組。有人能幫我嗎?

+0

您的'response'字符串包含您向我們展示的對象,這意味着您無法從中創建'JSONArray'。你應該從'response'創建一個對象,獲得由'cliente'鍵索引的數組,然後處理這個列表中的每個條目。 – bvidal

+0

所以我必須處理我的JSON對象,而不是一個數組?會給我一個例子嗎?是我是json的新手,只能做到只有一個客戶,而不是一個數字.. – zullyB

+0

看看下面的答案,這將是一個好開始 – bvidal

回答

1

您最初通過cliente獲得JSONObject。其餘數據爲JSONArray。解析你的對象並獲得Array的句柄。遍歷數組並獲取單個元素:

JSONParser parser = new JSONParser(); 

Object parsed = parser.parse(response.toString()); 
JSONObject jsonObject = (JSONObject) parsed; 

JSONArray jsonArray = (JSONArray) jsonObject.get("cliente"); 

for (Object obj : jsonArray) { 
    if (obj instanceof JSONObject) { 
     JSONObject cpf = (JSONObject) obj; 
     System.out.println(cpf.get("cpf").toString()); 
    } 
} 
+0

我做了,然後我將不得不創建此解析器類json ,有任何網站顯示如何組裝正確的JSON結構的Android? – zullyB

+0

@zullyB這些是上面使用的json類:'import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser;' – PopoFibo

+0

好的,veryyyyyyy謝謝! – zullyB