测试它是JSONObject还是JSONArray

Tan*_*ang 50 java json

我有一个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"
                       }
    }
}
Run Code Online (Sandbox Code Playgroud)

或类似的东西

{"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"
                      }
            }]
}   
Run Code Online (Sandbox Code Playgroud)

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

JSONObject json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream     
JSONArray  interventionJsonArray = json.getJSONArray("intervention");
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,上面的方法不起作用,因为流中只有一个元素.如何检查流是一个object还是一个array

我试过json.length()但它没有用..

谢谢

T.J*_*der 115

这样的事情应该这样做:

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
}
Run Code Online (Sandbox Code Playgroud)

这样做的好处是JSONObject只需从main获取属性值一次.因为获取属性值涉及遍历哈希树或类似物,这对于性能(对于它的价值)是有用的.


Nat*_*n Q 13

也许像这样的支票?

JSONObject intervention = json.optJSONObject("intervention");
Run Code Online (Sandbox Code Playgroud)

如果干预对象不是JSON对象,则返回一个JSONObject或者null.接下来,这样做:

JSONArray interventions;
if(intervention == null)
        interventions=jsonObject.optJSONArray("intervention");
Run Code Online (Sandbox Code Playgroud)

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


azw*_*bar 10

为简单起见,您只需从服务器结果中检查第一个字符串即可.

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
}
Run Code Online (Sandbox Code Playgroud)

这个技巧只是基于JSON格式的String {foo : "bar"}(对象)或 [ {foo : "bar"}, {foo: "bar2"} ](数组)


小智 7

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

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
Run Code Online (Sandbox Code Playgroud)

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