Java 解析JSON Android ArrayList

Java 解析JSON Android ArrayList,java,android,json,arraylist,jsonobject,Java,Android,Json,Arraylist,Jsonobject,如果我的问题标题有点误导,我很抱歉 我创建了一个POJO来保存关于用户的胆固醇信息(HDL、LDL、甘油三酯、单位等)。现在我想使用我的JSONObject创建一个ArrayList,以便生成一些数据点 我的JSONObject包含以下内容: { "cholesterol": [ { "date": "2014-01-01", "hdl": "56464.0", "ldl": "46494.0", "triGlycaride

如果我的问题标题有点误导,我很抱歉

我创建了一个POJO来保存关于用户的胆固醇信息(HDL、LDL、甘油三酯、单位等)。现在我想使用我的JSONObject创建一个ArrayList,以便生成一些数据点

我的JSONObject包含以下内容:

{
"cholesterol": [
    {
        "date": "2014-01-01",
        "hdl": "56464.0",
        "ldl": "46494.0",
        "triGlycaride": "0.0",
        "uid": "email@email.com",
        "unit": "mg"
    },
    {
        "date": "2014-01-01",
        "hdl": "5.0",
        "ldl": "5.0",
        "triGlycaride": "0.0",
        "uid": "email@email.com",
        "unit": "mg"
    },
    {
        "date": "2014-01-01",
        "hdl": "6.0",
        "ldl": "6.0",
        "triGlycaride": "0.0",
        "uid": "email@email.com",
        "unit": "mg"
    }
]
}
我的问题是,如何遍历这个JSON对象?我想使用for each,并在每次迭代中创建一个新对象添加到ArrayList。。。你有什么建议吗? 注意:我以前从未使用过JSONObject,因此不太熟悉它的用法


编辑:谢谢大家,这正是我想要的。我需要更加熟悉JSON操作。我也会调查格森

是时候学习一些JSON操作了:

JSONArray array = yourJsonObject.optJSONArray("cholesterol");
if (array != null) {
    for (int i=0; i< array.length; i++) {
        JSONObject object = array.optJSONObject(i);
        if (object != null) {
            // this is where you manipulate all the date, hdl, ldl...etc
        }
    }
}
JSONArray数组=yourJsonObject.optJSONArray(“胆固醇”);
if(数组!=null){
for(int i=0;i

您还应该在访问json之前检查null,现在是学习一些json操作的时候了:

JSONArray array = yourJsonObject.optJSONArray("cholesterol");
if (array != null) {
    for (int i=0; i< array.length; i++) {
        JSONObject object = array.optJSONObject(i);
        if (object != null) {
            // this is where you manipulate all the date, hdl, ldl...etc
        }
    }
}
JSONArray数组=yourJsonObject.optJSONArray(“胆固醇”);
if(数组!=null){
for(int i=0;i

您还应该在访问json之前检查null如果我理解正确,您想创建POJO的ArrayList吗?我假设在POJO类中有getter和setter。在靠近顶部的某个位置初始化ArrayList,如下所示

private ArrayList<CholesterolInformation> mCholesterol;
JSONobject data = new JSONObject(jsonStringData);
JSONArray cholesterol = data.getJSONArray("cholesterol");
for(int i = 0; i < cholesterol.length; i++)
{
    JSONObject object = cholesterol.getJSONObject(i);
    // Create a new object of your POJO class
    CholesterolInformation ci = new CholesterolInformation();
    // Get value from JSON
    String date = object.getString("date");
    // Set value to your object using your setter method
    ci.setDate(date);
    String hdl = object.getString("hdl");
    ci.setHdl(hdl);
    .....
    .....
    // Finally, add the object to your arraylist
    mCholesterol.add(ci);
}
private ArrayList mCholesterol;
现在,像这样解析json

private ArrayList<CholesterolInformation> mCholesterol;
JSONobject data = new JSONObject(jsonStringData);
JSONArray cholesterol = data.getJSONArray("cholesterol");
for(int i = 0; i < cholesterol.length; i++)
{
    JSONObject object = cholesterol.getJSONObject(i);
    // Create a new object of your POJO class
    CholesterolInformation ci = new CholesterolInformation();
    // Get value from JSON
    String date = object.getString("date");
    // Set value to your object using your setter method
    ci.setDate(date);
    String hdl = object.getString("hdl");
    ci.setHdl(hdl);
    .....
    .....
    // Finally, add the object to your arraylist
    mCholesterol.add(ci);
}
JSONobject数据=新的JSONobject(jsonStringData);
JSONArray胆固醇=data.getJSONArray(“胆固醇”);
对于(int i=0;i
如果我理解正确,是否要创建POJO的ArrayList?我假设在POJO类中有getter和setter。在靠近顶部的某个位置初始化ArrayList,如下所示

private ArrayList<CholesterolInformation> mCholesterol;
JSONobject data = new JSONObject(jsonStringData);
JSONArray cholesterol = data.getJSONArray("cholesterol");
for(int i = 0; i < cholesterol.length; i++)
{
    JSONObject object = cholesterol.getJSONObject(i);
    // Create a new object of your POJO class
    CholesterolInformation ci = new CholesterolInformation();
    // Get value from JSON
    String date = object.getString("date");
    // Set value to your object using your setter method
    ci.setDate(date);
    String hdl = object.getString("hdl");
    ci.setHdl(hdl);
    .....
    .....
    // Finally, add the object to your arraylist
    mCholesterol.add(ci);
}
private ArrayList mCholesterol;
现在,像这样解析json

private ArrayList<CholesterolInformation> mCholesterol;
JSONobject data = new JSONObject(jsonStringData);
JSONArray cholesterol = data.getJSONArray("cholesterol");
for(int i = 0; i < cholesterol.length; i++)
{
    JSONObject object = cholesterol.getJSONObject(i);
    // Create a new object of your POJO class
    CholesterolInformation ci = new CholesterolInformation();
    // Get value from JSON
    String date = object.getString("date");
    // Set value to your object using your setter method
    ci.setDate(date);
    String hdl = object.getString("hdl");
    ci.setHdl(hdl);
    .....
    .....
    // Finally, add the object to your arraylist
    mCholesterol.add(ci);
}
JSONobject数据=新的JSONobject(jsonStringData);
JSONArray胆固醇=data.getJSONArray(“胆固醇”);
对于(int i=0;i
按照Eric的建议使用,因为您已经创建了POJO

Gson gson = new Gson();
Type type = new TypeToken<List<POJO>>() {}.getType();
List<POJO> mList = gson.fromJson(your_json_string_here, type);
Gson-Gson=new-Gson();
Type Type=new-TypeToken(){}.getType();
List mList=gson.fromJson(此处输入您的字符串);
按照Eric的建议使用,因为您已经创建了POJO

Gson gson = new Gson();
Type type = new TypeToken<List<POJO>>() {}.getType();
List<POJO> mList = gson.fromJson(your_json_string_here, type);
Gson-Gson=new-Gson();
Type Type=new-TypeToken(){}.getType();
List mList=gson.fromJson(此处输入您的字符串);

如果您想避免迭代,可以检查将JSON映射到POJO。如果你想避免迭代,你可以选择将你的JSON映射到你的POJO。也检查一下这个。