3

I am trying to create JSON object with the following format.

{
"tableID": 1,
"price": 53,
"payment": "cash",
"quantity": 3,
"products": [
    {
        "ID": 1,
        "quantity": 1
    },
    {
        "ID": 3,
        "quantity": 2
    }
]

}

  1. I know how to do this statically using JSONObject and JSONArray. But I need a more dynamic way cause the products array must be implemented so it has many objects not just 2.

  2. IS there a way to delete the contents of a JSONObject? eg I have the following JSONobject

    { "ID": 3, "quantity": 2 }

Could I somehow erase its values so i can reuse the object in an iteration?

Giannis
  • 121
  • 2
  • 4
  • 13
  • Since product is an array, you could iterate it and if you know the id to delete then remove it. – lsiva Jul 11 '17 at 19:54
  • Thank your for replying but I am not trying to delete anything from the array. I am trying to build a JSONObject like the one I posted – Giannis Jul 11 '17 at 19:58

5 Answers5

4

To your first question, you can build the above json dynamically like this.

JSONObject jsonObject = new JSONObject();
jsonObject.put("tableID", 1);
jsonObject.put("price", 53);
jsonObject.put("payment", "cash");
jsonObject.put("quantity", 3);

JSONArray products = new JSONArray();

//product1
JSONObject product1 = new JSONObject();
product1.put("ID", 1);
product1.put("quantity", 1);
products.put(product1); //add to products

//product3
JSONObject product3 = new JSONObject();
product3.put("ID", 3);
product3.put("quantity", 2);
products.put(product3); //add to products

jsonObject.put("products", products); //add products array to the top-level json object

To the second question, you can remove an element of a JSONObject if you know its name.

jsonObject.remove("tableID"); // remove the tableID key/value pair

Or if you want to remove a specific element of a JSONArray then you have to know its position in the collection

jsonObject.getJSONArray("products").remove(1); //removes the second item in the collection which is the product3
kws
  • 896
  • 7
  • 11
0

Try constructing your JSON data as a string:

String json = "{" +
            "\"tableID\": 1," +
            "\"price\": 53," +
            "\"payment\": \"cash\"," +
            "\"quantity\": 3," +
            "\"products\": [";

for (int i = 0; i < 100; i++) {
    json += "{ \"ID\": 3, \"quantity\": 2 }";
    if(i != 100) json += ",";    
}

json += "]}";

and then create your JSONObject:

JSONObject jsonObject = new JSONObject(json);

I do not know what you exactly want to do, but I am guessing it is something like below:

for(int i = 0; i < products.size(); i++) {
    json += "{" +
          "\"ID\": " + products.get(i).getId() + "," +
          "\"quantity\": " + products.get(i).getQuantity() + " }";
    if(i != products.size() - 1) json += ",";
}

Note: See kws's answer for a more readable way to do it.

0

Yes, being an object you can change its content. Answering your question, it would be so.

JSONArray jsArray = (JSONArray) json.get("products");

JSONObject js1 = jsArray.getJSONObject(0);
System.out.println("0: "+js1);
jsArray.remove(0);

JSONObject js2 = jsArray.getJSONObject(0);
System.out.println("1: "+js2);
jsArray.remove(0);

Therefore, you can iterate that array or make it into a list, according to your preference

VIX
  • 565
  • 3
  • 13
0

For the first question

You can use this website to create the java classes and it will create 2, one for the attributes (tableId...) and another one for the products and simply contain array of Products in the main class

For the second question

you can just ignore it products[0] and ignore products[1](Id:3)

Anis LOUNIS
  • 4,472
  • 5
  • 30
  • 58
0

Woops, I wrote a whole long answer in js instead of java.

Simply use a HashMap and the GSON library from Google.

See here: How can I convert JSON to a HashMap using Gson?

You create your object dynamically as a hashMap, adding products and removing them at will. Then you turn the object into a JSON string with toJson() method.

(or start from any JSON string read into a HashMap with fromJson() and then manipulate it at will and return it to a string.

pashute
  • 3,791
  • 3
  • 35
  • 59