2012-10-09 37 views
1
[  
    { 
     "vid": "45", 
     "name": "hello", 
     "description": "hello", 
     "help": "hello", 
     "relations": "1", 
     "hierarchy": "1", 
     "multiple": "1", 
     "required": "0", 
     "tags": "1", 
     "module": "taxonomy", 
     "weight": "7", 
     "nodes": { 
      "hidrupal": "hidrupal", 
      "page": "page", 
      "recipee": "recipee", 
      "story": "story" 
     }, 
     "uri": "http://andrd.yarrait.com/drupal6/android/vocabulary/45" 
    }, 
    { 
     "vid": "48", 
     "name": "sample", 
     "description": "asldfawi iawbfuiasguda jasbf jasfujad jashbdfajsdgawidba kjascbi9wa jhasbjabcuasv askjh bw ajs bjsbjda jsawioabfscja awjdbfuwga ajcsjfgjaw ajdfajs fj agfjweu8 ajsfbjw u", 
     "help": "ajfawyg ahbcoiw ygajc jawhbu bua jagbwjwcwug ua jwsajwiuhcg ha bwujagua jajdwgugd uma jau wyg j h jja hg ug auj gwj", 
     "relations": "1", 
     "hierarchy": "1", 
     "multiple": "1", 
     "required": "0", 
     "tags": "1", 
     "module": "taxonomy", 
     "weight": "7", 
     "type": null, 
     "nodes": [ ], 
     "uri": "http://andrd.yarrait.com/drupal6/android/vocabulary/48" 
    } 
] 

我有2 JSONArray在於,它具有「節點」對象,有時有值和有時候具有空,因此如何檢查這個「節點」對象是否有空值我已JSON在存在一個其它JSON對象所以如何檢查第二JSON對象爲空

我試試這樣。請檢查,並告訴我如何解決這個問題JSONArray

當我不是因爲得到節點對象空值的上市
mJSONArray = new JSONArray(response); 

for (int i = 0; i < mJSONArray.length(); i++) 
{ 
    JSONObject mJSONObject = mJSONArray.getJSONObject(i); 

    mArrayListName.add(mJSONObject.getString(Constant.VocabularyJsonKey.NAME)); 
    mArrayListvid.add(mJSONObject.getString(Constant.VocabularyJsonKey.VID)); 

    if(!mJSONObject.isNull(Constant.VocabularyJsonKey.CONTENT_TYPE)) 
    { 
     JSONObject contentTypeJSONObject = mJSONObject.getJSONObject(Constant.VocabularyJsonKey.CONTENT_TYPE); 
     JSONArray contentTypeJSONArray = contentTypeJSONObject.toJSONArray(contentTypeJSONObject.names()); 

     //for getting content types and set as comma separated string 
     for (int j = 0; j < contentTypeJSONArray.length(); j++) 
     { 
     if(j == contentTypeJSONArray.length()-1) 
      content_types = content_types.concat(contentTypeJSONArray.getString(j).toString()); 
     else 
      content_types = content_types.concat(contentTypeJSONArray.getString(j).toString() + ", "); 
     } 
    } 
    mArrayListCtype.add(content_types); 
} 

出錯。錯誤是 org.json.JSONException:類型爲org.json.JSONArray的節點上的值[]無法在org.json.JSONObject的org.json.JSON.typeMismatch(JSON.java:96)處轉換爲JSONObject。 getJSONObject(JSONObject.java:573)

+2

你試過了嗎? 'OuterObject.innerObject === null'。在你的case object [0] .nodes中=== null –

+0

JSONObject jsonObject; jsonObject.isNull(「name」); – Pasha

回答

0

嘗試 nodes.length == 0 //空

0

不是節點一個JSONArray ??因爲在第二個數據集中有「節點」:[]。

只是用此來檢查:

parentJSON.getJSONArray("nodes").length() == 0; 

如果一個JSONObject檢查:

parentJSON.getJSONObject("nodes").length() == 0; 

方法長度()上一個JSONObject給出了的JSONObject namevaluepairs中的數量。

0

我覺得類型應該是這個樣子的JSON數組的

[

//這是你的第一個對象

{ 「類別」:

{ 「CAT_ID」: 「1」,「cat_title」:「Advertising & Media」}},

//這是您的第二個對象

{ 「類別」:

{ 「CAT_ID」: 「2」, 「cat_title」: 「汽車&海洋」}}

]

,你通過下面的代碼檢索:

for (int i = 0; i < jsonArray.length(); i++) { 

        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        String outerstr = jsonObject.getString("category"); 

        JSONObject json = new JSONObject(outerstr); 

        String id = json.getString("cat_id"); 
        String title = json.getString("cat_title"); 

        Log.i("title ", title);     
        Log.i("desc ", id); 

        yourList.add(new yourClassObject(id,title)); 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       }