2014-01-19 3 views
-4

이 JSON을 읽었습니까? 아직 JSON을 사용하지 않아서 데이터를 가져올 수 없습니다.데이터 가져 오기 JSON Android 오류

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); 
    } 

} 

오류는 객체 배열을 변환 할 수 없습니다 말 : 여기

{ 
    "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" 
     } 
    ] 
} 

내가 데이터를 가져 오는 방법입니다 : 여기

는 JSON이다. 누군가 나를 도울 수 있습니까?

+0

'response' 문자열은 여러분이 우리에게 보여주고있는 것에서 객체를 보유하고 있습니다. 즉, 여러분이 우리에게 보여주고있는 객체를 포함하고 있습니다. 즉,'JSONArray'를 만들 수 없습니다. 'response'로부터 객체를 생성하고,'cliente' 키에 의해 색인 된 배열을 얻은 다음이리스트의 각 엔트리를 처리해야합니다. – bvidal

+0

그래서 json 객체와 배열을 처리해야합니까? 나에게 모범을 보일까? 나는 json을 처음 접했을 뿐이고 숫자가 아닌 고객과 만 처리 할 수 ​​있습니다. – zullyB

+0

다음은 좋은 시작이 될만한 답변을 찾으십시오 – bvidal

답변

1

처음에는 cliente을 통해 JSONObject를 얻습니다. 나머지 데이터는 JSONArray입니다. 개체를 구문 분석하고 배열의 핸들을 가져옵니다. 배열을 반복하고 개별 요소를 얻으십시오 :

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의 올바른 구조를 조립하는 방법을 보여줄 수있는 사이트가 있습니까? – zullyB

+0

@zullyB 이들은 위에서 사용 된 json 클래스이다 :'import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; ' – PopoFibo

+0

ok, veryyyyyyy 감사합니다! – zullyB