I got the following json:

{
    "ID": "1234567",
    "dangereousCargo": true,
    "numberOfPassangers": 164,
    "cargo": [
        {
            "type": "Oil",
            "amount": 8556
        },
        {
            "type": "Chemicals",
            "amount": 5593
        }
    ]
}

From this question, I understood that it is possible to get the cargoList out of the jsonObject (if that list contains a certain type of object). But how do I get the seperate cargoObjects out of that list?

+Do the variable names of the jsonstring have to correspond with the variable names in my CargoClass? What if the jsonObject only contains type and amount and my CargoClass has more attributes?

3 Answers

1
Gianni B. On Best Solutions

But how do I get the seperate cargoObjects out of that list?

String jsonString = "{ ... }";
JSONObject json = new JSONObject(jsonString);
JSONArray cargoList = json.getJSONArray("cargo");
for(JSONObject cargo : cargoList)
{
   //do something with your cargo element
}

Do the variable names of the jsonstring have to correspond with the variable names in my CargoClass?

If you use the the get method from the JSONObject, you have to specify the exact name of the attribute in your jsonString. Following the example above:

String cargoType = cargo.getString("type");

By the way, if you want to use your already defined CargoClass, you need a Deserializer and all the attributes on your JSON must be the all present and all the same on your CargoClass: I suggest you to take a look at other SOs questions like this one.

What if the jsonObject only contains type and amount and my CargoClass has more attributes?

The other attributes will be initialize in base of your class declaration

3
ThomasThiebaud On

You can iterate throws the JSONArray which represents your cargo list doing (not tested)

JSONObject json = new JSONObject(jsonString);
JSONArray cargoList = json.getJSONArray("cargo");

for(int i=0; i< cargoList.length(); i++) {
    JSONObject cargo = cargoList.getJSONObject(i);
    //Do something with cargo
}
0
basic On

Using JSON Simple it is very easy to parse and read your data from your JSON. As other users have said there are a plethora of libraries out there that can accomplish this. Below is a tested example of your code.

    public static void main(String[] args) throws JSONException {
    String json = "{\n"
            + "    ID: 1234567,\n"
            + "    dangereousCargo: true,\n"
            + "    numberOfPassangers: 164,\n"
            + "    cargo: [\n"
            + "        {\n"
            + "            type: Oil,\n"
            + "            amount: 8556\n"
            + "        },\n"
            + "        {\n"
            + "            type: Chemicals,\n"
            + "            amount: 5593\n"
            + "        }\n"
            + "    ]\n"
            + "}";

    JSONObject data = new JSONObject(json);

    int id = data.getInt("ID");
    boolean danger = data.getBoolean("dangereousCargo");
    int numOfPassengers = data.getInt("numberOfPassangers");

    System.out.println("Current ID: " + id + "\n"
            + "Is Dangerous: " + danger + "\n"
            + "Number of Passengers: " + numOfPassengers + "\n");
    JSONArray cargo = data.getJSONArray("cargo");

    NumberFormat currency = NumberFormat.getCurrencyInstance();
    for (int i = 0; i < cargo.length(); i++) {
        JSONObject cargoObject = cargo.getJSONObject(i);
        String type = cargoObject.getString("type");
        double amount = cargoObject.getDouble("amount");
        System.out.println("Current Type: " + type);
        System.out.println("Current Amount: " + currency.format(amount));
    }

  }

}

The above code gives us the following output:

enter image description here

Once you have your data you can really do whatever you want with it.

Libraries

JSONSimple-https://code.google.com/p/json-simple/

GSON-https://github.com/google/gson