0

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!

1
  • 3
    If you want to avoid the iteration you can check Gson to map your JSON to your POJO. Check this tutorial too. Commented Jul 5, 2015 at 4:26

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.