![](/img/trans.png)
[英]Method to cast Object to JSONObject or JSONArray depending on the Object
[英]JSONArray equivalent to JSONObject's .has() method?
我一直在用這個:
JSONObject json = new JSONObject(output);
if (json.has("errors"))
{
}
現在我有一個 JSONArray ......我如何檢查它是否有一個名為“錯誤”的孩子?
嘗試這個:
JSONArray array;
for(int i = 0; i < array.length(); i++){
JSONObject json = array.getJSONObject(i);
if (json.has("errors")){
//magic
}
}
使用像這樣的 try catch 塊:
boolean arrayexists = true;
try{
json.has("errors") }
catch(Exception e){
arrayexists = false;}
if (arrayexists) {
//majic
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.