确定的JSON是否为一个的JSONObject或JSONArrayJSON、JSONObject、JSONArray

2023-09-11 20:30:53 作者:威龙丶断魂神狙

我会接受任何一个JSON对象或数组的服务器,但我不知道它会。我需要用JSON工作,但要做到这一点,我需要知道,如果它是一个对象或数组。

I am going to receive either a JSON Object or Array from server, but I have no idea which it will be. I need to work with the json, but to do so, I need to know if it is an Object or an Array.

我的工作与Android。

I am working with Android.

没有任何一个有这样的好办法?我不出来,它应该是pretty的方便。

Does any one have a good way of doing this?? I can't figure it out, it should be pretty easy.

感谢

推荐答案

我找到更好的办法来确定:

I found better way to determine:

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

标记生成器能够返回多种类型:http://developer.android.com/reference/org/json/JSONTokener.html#nextValue()