简体   繁体   English

JSON Java检查元素是JSONArray或JSONObject

[英]JSON Java check element is a JSONArray or JSONObject

How to check whether an element is a JSONArray or JSONObject. 如何检查元素是JSONArray还是JSONObject。 I wrote the code to check, 我写了代码来检查,

if(jsonObject.getJSONObject("Category").getClass().isArray()) {

} else {

}

In this case if the element 'category' is JSONObject then it work fine but if it contains an array then it throw exception: JSONArray cannot be converted to JSONObject. 在这种情况下,如果元素'category'是JSONObject,那么它工作正常,但如果它包含一个数组,那么它会抛出异常:JSONArray无法转换为JSONObject。 Please help. 请帮忙。 Thanks. 谢谢。

Yes, this is because the getJSONObject("category") will try to convert that String to a JSONObject what which will throw a JSONException . 是的,这是因为getJSONObject("category")将尝试将该String转换为JSONObject,这会抛出JSONException You should do the following: 您应该执行以下操作:

Check if that object is a JSONObject by using: 使用以下命令检查该对象是否为JSONObject

   JSONObject category=jsonObject.optJSONObject("Category");

which will return a JSONObject or null if the category object is not a json object. 如果category对象不是json对象,它将返回JSONObjectnull Then you do the following: 然后执行以下操作:

   JSONArray categories;
   if(category == null)
        categories=jsonObject.optJSONArray("Category");

which will return your JSONArray or null if it is not a valid JSONArray . 如果它不是有效的JSONArray ,它将返回您的JSONArray或null。

Even though you have got your answer, but still it can help other users... 即使你有答案,但它仍然可以帮助其他用户......

 if (Law.get("LawSet") instanceof JSONObject)
 {
    JSONObject Lawset = Law.getJSONObject("LawSet");                        
 }
 else if (Law.get("LawSet") instanceof JSONArray)
{
    JSONArray Lawset = Law.getJSONArray("LawSet");
}

Here Law is other JSONObject and LawSet is the key which you want to find as JSONObject or JSONArray . 这里的Law是其他JSONObjectLawSet是你想要找到的JSONObject or JSONArray

String data = "{ ... }";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
  //you have an object
else if (json instanceof JSONArray)
  //you have an array

tokenizer is able to return more types: http://developer.android.com/reference/org/json/JSONTokener.html#nextValue() tokenizer能够返回更多类型: http//developer.android.com/reference/org/json/JSONTokener.html#nextValue()

           if (element instanceof JSONObject) {

                Map<String, Object> map = json2Java.getMap(element
                        .toString());
                if (logger.isDebugEnabled()) {
                    logger.debug("Key=" + key + " JSONObject, Values="
                            + element);
                }
                for (Entry<String, Object> entry : map.entrySet()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug(entry.getKey() + "/"
                                + entry.getValue());
                    }
                    jsonMap.put(entry.getKey(), entry.getValue());
                }
            }

You can use instanceof to check the type of the result that you get. 您可以使用instanceof来检查所获得的结果的类型。 Then you can handle it as you wish. 然后你可以随意处理它。

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

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