android - 确定 JSON 是 JSONObject 还是 JSONArray

标签 android arrays json object

我将从服务器接收 JSON 对象或数组,但我不知道它会是哪个。我需要使用 JSON,但要这样做,我需要知道它是对象还是数组。

我正在使用 Android。

有没有人有这样的好方法?

最佳答案

我找到了更好的方法来确定:

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 能够返回更多类型:http://developer.android.com/reference/org/json/JSONTokener.html#nextValue ()

关于android - 确定 JSON 是 JSONObject 还是 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118708/

相关文章:

android - 在文本 int TextView 中插入水平线

android - 如何修复 ViewRoot#updateBidiOptions 中的 NPE?

javascript - 过滤数组中的数据并返回调整后的数组

android - 在android中将JsonArray转换为byte[]数组

json - "The property cannot be found on this object. Verify that the property exists."属性肯定存在

android - 在 Android Pie 上启动 context.startService(intent)

Android canGoBack 始终为 false

php - codeigniter如何从 Controller 传递base_url()

C:函数未返回正确的值

json - 如何在 DetailViewController 中获取 JSONData? swift