Android 如何获取json数组

Android 如何获取json数组,android,Android,我需要给定json数据中的custac数组。但是我不知道怎么从那打电话给库斯塔克。我想获取数组中的custac值。有人能帮忙吗 这是我的密码 ArrayList<CustomerPayment> customerPayments = new ArrayList<CustomerPayment>(); try { JSONArray resultVal = response.getJSONArray("Data"); int count=resultVa

我需要给定json数据中的custac数组。但是我不知道怎么从那打电话给库斯塔克。我想获取数组中的custac值。有人能帮忙吗 这是我的密码

ArrayList<CustomerPayment> customerPayments = new 
ArrayList<CustomerPayment>();
try {
    JSONArray resultVal = response.getJSONArray("Data");

    int count=resultVal.length();
    for(int i=0;i<count;i++)
    {
        CustomerPayment payment = new CustomerPayment(resultVal.getJSONObject(i));
        customerPayments.add(payment);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

你必须做这样的东西

JSONObject jsonObject = new JSONObject(response);

JSONArray resultVal = jsonObject.getJSONArray("Data");

JSON数据是解析为字符串的对象,主要用于NoSQL目的,并且可以解析为对象。是一个易于解析JSON数据的库

如果将jsonData作为包含数据的响应字符串,那么下面的代码可以解析数组列表中的custac。但是,首先必须为数据创建一个对象

数据必须包含CustomerID、FirstName等属性

对于你的情况

class Data{
    private String CustomerID;
    private String FirstName;
    private String LastName;
    private String NickName;
    private int FundAmount;
    private ArrayList<Custac> custac;
    class Custac{
        // Write your attributes as shown above.
    }
}
试试这个代码

JSONArray custac;
 try {

    JSONArray resultVal = jsonObject.getJSONArray("Data");
    for (int i = 0; i < resultVal.length() - 1; i++) {
        jsonObject = resultVal.getJSONObject(i);
        custac = jsonObject.getJSONArray("custac");

        Log.d("TAG, custac + "");
    }
} catch (JSONException e) {
    e.printStackTrace();
}

@AbhayBohra能否请您解释在循环内部使用resultVal.geti.getJSONArraycustac@SRM首先,请检查您的JSON,它是无效的response@Nero是的,它是正确的。我只复制了一些可能的副本
class Data{
    private String CustomerID;
    private String FirstName;
    private String LastName;
    private String NickName;
    private int FundAmount;
    private ArrayList<Custac> custac;
    class Custac{
        // Write your attributes as shown above.
    }
}
JSONArray custac;
 try {

    JSONArray resultVal = jsonObject.getJSONArray("Data");
    for (int i = 0; i < resultVal.length() - 1; i++) {
        jsonObject = resultVal.getJSONObject(i);
        custac = jsonObject.getJSONArray("custac");

        Log.d("TAG, custac + "");
    }
} catch (JSONException e) {
    e.printStackTrace();
}