简体   繁体   English

检查作为 JSONObject 数组的字符串是否为空

[英]checking if a string that is a array of JSONObject is empty

So I am receiving, a jsonobject from an api call.所以我收到了来自 api 调用的 jsonobject。

inside the jsonobject there is a field that can hold an array of JSONObjects在 jsonobject 里面有一个字段可以保存一个 JSONObjects 数组

for example例如

{ "order":[]} { “命令”:[]}

how can I check if the array is empty or not?如何检查数组是否为空?

JSONSimple library JSONSimple 库

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[]预期结果:json[] 的大小

Thanks谢谢

You cannot cast an array of JsonObject (JSONObject[]) to a List of JSONObject (List) - they are 2 different class with 2 different heirarchy.您不能将 JsonObject (JSONObject[]) 数组转换为 JSONObject (List) 列表 - 它们是具有 2 个不同层次结构的 2 个不同类。 Cast it to JSONObject[], and since now it is an array and not a list, use length, not size().将它转换为 JSONObject[],因为现在它是一个数组而不是一个列表,所以使用 length,而不是 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM