确定的JSON是否为一个的JSONObject或JSONArray [英] Determine whether JSON is a JSONObject or JSONArray

查看:147
本文介绍了确定的JSON是否为一个的JSONObject或JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会接受任何一个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

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

这篇关于确定的JSON是否为一个的JSONObject或JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆