new*_*rld 234
我找到了更好的方法来确定:
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
Run Code Online (Sandbox Code Playgroud)
tokenizer能够返回更多类型:http://developer.android.com/reference/org/json/JSONTokener.html#nextValue()
nic*_*ild 53
有几种方法可以做到这一点:
{
,你正在处理a JSONObject
,如果它是a [
,你正在处理a JSONArray
.Object
),那么您可以进行instanceof
检查. yourObject instanceof JSONObject
.如果yourObject是JSONObject,则返回true.这同样适用于JSONArray.sou*_*els 13
这是我在Android上使用的简单解决方案:
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
}
} else {
// Do nothing or throw exception if "data" is a mandatory field
}
Run Code Online (Sandbox Code Playgroud)
提出另一种方式:
if(server_response.trim().charAt(0) == '[') {
Log.e("Response is : " , "JSONArray");
} else if(server_response.trim().charAt(0) == '{') {
Log.e("Response is : " , "JSONObject");
}
Run Code Online (Sandbox Code Playgroud)
这server_response
是来自服务器的响应字符串
归档时间: |
|
查看次数: |
79544 次 |
最近记录: |