java判断jsonarray是否为数组_java-测试它是JSONObject还是JSONArray

java-测试它是JSONObject还是JSONArray

我有一个json流,可以像这样:

{"intervention":

{

"id":"3",

"subject":"dddd",

"details":"dddd",

"beginDate":"2012-03-08T00:00:00+01:00",

"endDate":"2012-03-18T00:00:00+01:00",

"campus":

{

"id":"2",

"name":"paris"

}

}

}

或类似的东西

{"intervention":

[{

"id":"1",

"subject":"android",

"details":"test",

"beginDate":"2012-03-26T00:00:00+02:00",

"endDate":"2012-04-09T00:00:00+02:00",

"campus":{

"id":"1",

"name":"lille"

}

},

{

"id":"2",

"subject":"lozlzozlo",

"details":"xxx",

"beginDate":"2012-03-14T00:00:00+01:00",

"endDate":"2012-03-18T00:00:00+01:00",

"campus":{

"id":"1",

"name":"lille"

}

}]

}

在我的Java代码中,请执行以下操作:

JSONObject json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream

JSONArray interventionJsonArray = json.getJSONArray("intervention");

在第一种情况下,上述方法不起作用,因为流中只有一个元素。如何检查流是json.length()还是array?

我尝试了json.length(),但是没有用。

谢谢

Tang asked 2020-01-28T18:48:13Z

7个解决方案

114 votes

这样的事情应该做到:

JSONObject json;

Object intervention;

JSONArray interventionJsonArray;

JSONObject interventionObject;

json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream

Object intervention = json.get("intervention");

if (intervention instanceof JSONArray) {

// It's an array

interventionJsonArray = (JSONArray)intervention;

}

else if (intervention instanceof JSONObject) {

// It's an object

interventionObject = (JSONObject)intervention;

}

else {

// It's something else, like a string or number

}

这样的好处是仅从主JSONObject获得属性值一次。 由于获取属性值涉及遍历哈希树或类似的哈希树,因此对于性能(价值而言)很有用。

T.J. Crowder answered 2020-01-28T18:48:29Z

13 votes

也许像这样的支票?

JSONObject intervention = json.optJSONObject("intervention");

如果干预对象不是JSON对象,则返回JSONArray或null。 接下来,执行以下操作:

JSONArray interventions;

if(intervention == null)

interventions=jsonObject.optJSONArray("intervention");

如果它是有效的JSONArray,它将返回一个数组,否则将给出null。

Nathan Q answered 2020-01-28T18:48:58Z

10 votes

为了简单起见,您可以只检查服务器结果中的第一个字符串。

String result = EntityUtils.toString(httpResponse.getEntity()); //this function produce JSON

String firstChar = String.valueOf(result.charAt(0));

if (firstChar.equalsIgnoreCase("[")) {

//json array

}else{

//json object

}

此技巧仅基于JSON格式的字符串{foo : "bar"}(对象)或[ {foo : "bar"}, {foo: "bar2"} ](阵列)

azwar_akbar answered 2020-01-28T18:49:22Z

2 votes

Object valueObj = uiJSON.get(keyValue);

if (valueObj instanceof JSONObject) {

this.parseJSON((JSONObject) valueObj);

} else if (valueObj instanceof JSONArray) {

this.parseJSONArray((JSONArray) valueObj);

} else if(keyValue.equalsIgnoreCase("type")) {

this.addFlagKey((String) valueObj);

}

// ITERATE JSONARRAY     私人void parseJSONArray(JSONArray jsonArray)抛出JSONException {         for(迭代器iterator = jsonArray.iterator(); iterator.hasNext();){             JSONObject对象=(JSONObject)iterator.next();             this.parseJSON(object);         }     }

Sagar Jadhav answered 2020-01-28T18:49:43Z

0 votes

我没有试过,但也许...

JsonObject jRoot = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream

JsonElement interventionElement = jRoot.get("intervention");

JsonArray interventionList = new JsonArray();

if(interventionElement.isJsonArray()) interventionList.addAll(interventionElement.getAsJsonArray());

else interventionList.add(interventionElement);

如果它是JsonArray对象,则只需使用getAsJsonArray()进行强制转换。 如果不是,则为单个元素,因此只需添加它。

无论如何,您的第一个示例已损坏,您应该要求服务器的所有者对其进行修复。 JSON数据结构必须一致。 不仅仅是因为有时干预仅包含1个元素,它也不必是数组。 如果只有1个元素,它将是只有1个元素的数组,但仍必须是一个数组,以便客户端可以使用始终相同的模式对其进行解析。

user1930830 answered 2020-01-28T18:50:12Z

0 votes

//returns boolean as true if it is JSONObject else returns boolean false

public static boolean returnBooleanBasedOnJsonObject(Object jsonVal){

boolean h = false;

try {

JSONObject j1=(JSONObject)jsonVal;

h=true;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

if(e.toString().contains("org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject")){

h=false;

}

}

return h;

}

haroon answered 2020-01-28T18:50:27Z

0 votes

您可以使用以下代码获取输入字符串的对象。

String data = "{ ... }";

Object json = new JSONTokener(data).nextValue();

if (json instanceof JSONObject)

//do something for JSONObject

else if (json instanceof JSONArray)

//do something for JSONArray

链接:[https://developer.android.com/reference/org/json/JSONTokener#nextValue]

Virendra Kachhi answered 2020-01-28T18:50:52Z

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值