Jack Ryan
Jack Ryan

Reputation: 1318

Parsing JSON Android ArrayList

I apologize if the title of my question is a bit misleading.

I created a POJO to hold CholesterolInformation about a user (HDL, LDL, Triglycerides, units, etc...). I now want to use my JSONObject to create an ArrayList so that I can generate some data points.

My JSONObject contains the following:

{
"cholesterol": [
    {
        "date": "2014-01-01",
        "hdl": "56464.0",
        "ldl": "46494.0",
        "triGlycaride": "0.0",
        "uid": "[email protected]",
        "unit": "mg"
    },
    {
        "date": "2014-01-01",
        "hdl": "5.0",
        "ldl": "5.0",
        "triGlycaride": "0.0",
        "uid": "[email protected]",
        "unit": "mg"
    },
    {
        "date": "2014-01-01",
        "hdl": "6.0",
        "ldl": "6.0",
        "triGlycaride": "0.0",
        "uid": "[email protected]",
        "unit": "mg"
    }
]
}

My question is, how would one go about iterating through this JSON Object? I would like to maybe use a for each, and create a new object to add to the ArrayList in each iteration... Do you have any advice or suggestions? Note: I have never used the JSONObject before, and thus am not too familiar with its usage.

EDIT: Thanks everybody, that was exactly what I was looking for. I need to get more familiar with JSON manipulation. And I will look into GSON as well!

Upvotes: 0

Views: 2092

Answers (3)

Scorpion
Scorpion

Reputation: 6901

Use GSON as suggested by Eric as you already created POJO.

Gson gson = new Gson();
Type type = new TypeToken<List<POJO>>() {}.getType();
List<POJO> mList = gson.fromJson(your_json_string_here, type);

Upvotes: 3

Bidhan
Bidhan

Reputation: 10687

If I understand you correctly, you want to create an ArrayList of your POJO? I assume you have getters and setters inside your POJO class. Initialize an ArrayList somewhere near the top like this

private ArrayList<CholesterolInformation> mCholesterol;

Now, parse through your json like this

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

Upvotes: 0

Nicholas Ng
Nicholas Ng

Reputation: 1468

It's time to learn some JSON manipulation:

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

you also should check for null before accessing the json

Upvotes: 1

Related Questions