确定JSON是JSONObject还是JSONArray

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

我正在使用Android。

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

我find了更好的方法来确定:

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

有几个方法可以做到这一点:

  1. 你可以在string的第一个位置检查字符(在修剪掉空白之后,因为它允许在有效的JSON中)。 如果是{ ,你正在处理一个JSONObject ,如果它是[ ,你正在处理一个JSONArray
  2. 如果你正在处理JSON(一个Object ),那么你可以做一个instanceof检查。 yourObject instanceof JSONObject 。 如果yourObject是JSONObject,这将返回true。 这同样适用于JSONArray。

这是我在Android上使用的简单解决scheme:

 JSONObject json = new JSONObject(jsonString); if (json.has("data")) { JSONObject dataObject = json.optJSONObject("data"); if (dataObject != null) { //Do things with object. } else { JSONArray array = json.optJSONArray("data"); //Do things with array } } 

呈现另一种方式:

 if(server_response.trim().charAt(0) == '[') { Log.e("Response is : " , "JSONArray"); } else if(server_response.trim().charAt(0) == '{') { Log.e("Response is : " , "JSONObject"); } 

这里server_response是来自服务器的响应string

我的方法将是从这个完全抽象。 也许有人认为这有用…

 import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class SimpleJSONObject extends JSONObject { private static final String FIELDNAME_NAME_VALUE_PAIRS = "nameValuePairs"; public SimpleJSONObject(String string) throws JSONException { super(string); } public SimpleJSONObject(JSONObject jsonObject) throws JSONException { super(jsonObject.toString()); } @Override public JSONObject getJSONObject(String name) throws JSONException { final JSONObject jsonObject = super.getJSONObject(name); return new SimpleJSONObject(jsonObject.toString()); } @Override public JSONArray getJSONArray(String name) throws JSONException { JSONArray jsonArray = null; try { final Map<String, Object> map = this.getKeyValueMap(); final Object value = map.get(name); jsonArray = this.evaluateJSONArray(name, value); } catch (Exception e) { throw new RuntimeException(e); } return jsonArray; } private JSONArray evaluateJSONArray(String name, final Object value) throws JSONException { JSONArray jsonArray = null; if (value instanceof JSONArray) { jsonArray = this.castToJSONArray(value); } else if (value instanceof JSONObject) { jsonArray = this.createCollectionWithOneElement(value); } else { jsonArray = super.getJSONArray(name); } return jsonArray; } private JSONArray createCollectionWithOneElement(final Object value) { final Collection<Object> collection = new ArrayList<Object>(); collection.add(value); return (JSONArray) new JSONArray(collection); } private JSONArray castToJSONArray(final Object value) { return (JSONArray) value; } private Map<String, Object> getKeyValueMap() throws NoSuchFieldException, IllegalAccessException { final Field declaredField = JSONObject.class.getDeclaredField(FIELDNAME_NAME_VALUE_PAIRS); declaredField.setAccessible(true); @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) declaredField.get(this); return map; } } 

现在永远摆脱这种行为…

 ... JSONObject simpleJSONObject = new SimpleJSONObject(jsonObject); ... 

以下是一个更基本的方法。

JsonArray本质上是一个List

JsonObject本质上是一个Map

 if (object instanceof Map){ JSONObject jsonObject = new JSONObject(); jsonObject.putAll((Map)object); ... ... } else if (object instanceof List){ JSONArray jsonArray = new JSONArray(); jsonArray.addAll((List)object); ... ... } 

的instanceof

Object.getClass()的getName()

对于那些在JavaScript中解决这个问题的人来说,以下这些工作对我来说(不知道它有多高效)。

 if(object.length != undefined) { console.log('Array found. Length is : ' + object.length); } else { console.log('Object found.'); }