-2

So I am receiving, a jsonobject from an api call.

inside the jsonobject there is a field that can hold an array of JSONObjects

for example

{ "order":[]}

how can I check if the array is empty or not?

JSONSimple library

JSONObject[] check = new JSONObject[0];

JSONObject g = new JSONObject();
g.put("test", check);

System.out.println(((List<JSONObject>)g.get("test")).size());

Actual result: error

Desired result: size of json[]

Thanks

Tim Willis
  • 140
  • 10
  • First, name is `"order"`, not `"test"`. Second, an array is a `JSONArray`, not a `List`. – Andreas Feb 26 '18 at 23:44
  • `org.json.simple.JSONArray` implements `List`. See [here](https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/JSONArray.java). – Izruo Feb 26 '18 at 23:47

1 Answers1

1

You cannot cast an array of JsonObject (JSONObject[]) to a List of JSONObject (List) - they are 2 different class with 2 different heirarchy. Cast it to JSONObject[], and since now it is an array and not a list, use length, not size().

    JSONObject[] check = new JSONObject[0];

    JSONObject g = new JSONObject();
    g.put("test", check);

    //System.out.println(((List<JSONObject>) g.get("test")).size());
    System.out.println(((JSONObject[]) g.get("test")).length);
Ari Singh
  • 1,228
  • 7
  • 12