簡體   English   中英

JSONArray 等價於 JSONObject 的 .has() 方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM