Java 获取数据JSON错误

Java 获取数据JSON错误,java,android,json,Java,Android,Json,你读过这个JSON了吗?我对JSON还不熟悉,无法获取数据 以下是JSON: { "cliente": [ { "cpf": "82334673382", "endereco": "ewkiewowieou", "id": "11", "nome": "Wagner Assis" }, { "cpf": "82334889

你读过这个JSON了吗?我对JSON还不熟悉,无法获取数据

以下是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);
    }

}

表示无法转换对象数组时出错。有人能帮我吗?

您最初通过
客户获得一个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());
    }
}

您的
响应
字符串包含您正在向我们展示的对象,这意味着您无法从中创建
JSONArray
。您应该从
响应中创建一个对象,通过键
cliente
索引数组,然后处理此列表中的每个条目。所以我必须处理json对象而不是数组?能给我举个例子吗?我是json新手,只处理一个客户,而不是一个数字。看看下面的答案,这是一个很好的开始,然后我必须创建这个json解析器类,有没有网站展示如何为android组装正确的json结构?@zullyB以上是正在使用的json类:
import org.json.simple.JSONArray;导入org.json.simple.JSONObject;导入org.json.simple.parser.JSONParser