2

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?

Community
  • 1
  • 1
Edito
  • 3,030
  • 13
  • 35
  • 67
  • You might consider the GSON User guide (https://sites.google.com/site/gson/gson-user-guide) if you plan to use this library for parsing your JSON (as the question you are referencing to uses GSON). – endowzoner Nov 09 '15 at 14:45

3 Answers3

3

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
}
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
1

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

Community
  • 1
  • 1
Gianni B.
  • 2,691
  • 18
  • 31
  • I was going to accept Maloubobola's answer, but yours is more complete and contains all the aspects of my question. I adjusted the code to my project which you can find here http://pastebin.com/618nAEAV . Final remark though, I see everybody using JSONObject etc. It does not work for me, are you using javax.json.JsonObject ? – Edito Nov 09 '15 at 16:55
  • No, I was assuming you had use the `org.json` [package](https://github.com/douglascrockford/JSON-java). I think this one is more readle but, in alternative, you can use the [GSon lbrary](https://github.com/google/gson) from Google – Gianni B. Nov 10 '15 at 09:37
0

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

basic
  • 3,348
  • 3
  • 21
  • 36