2013-05-13 22 views

回答

0

當然,你可以做到這一點。 看看的JSONObject參考:

http://www.json.org/javadoc/org/json/JSONObject.html

您可以使用keys()迭代來發現哪些元素是當前可用的節點內。

然後,您可以使用instanceof運算符來檢查某個節點是否爲JSONArrayString或其他。

+0

這只是帶回了一個對象,當有兩個對象和一個數組 – user1136994 2013-05-14 05:47:26

0

試試這個

public void analyzeJSON(String jsonString) { 
    JSONTokener tok = new JSONTokener(jsonString); 
    while(tok.more()) { 
     try { 
      Object item = tok.nextValue(); 
      if (item instanceof JSONObject) { 
       // JSON Object 
       analyzeJSON(item.toString()); 
      } else if (item instanceof JSONArray) { 
       // JSON Array 
       JSONArray array = (JSONArray) item; 
       for (int i = 0; i < array.length(); i++) { 
        Object subItem = array.get(i); 
        if (subItem instanceof JSONObject) { 
         // JSON Object 
         analyzeJSON(item.toString()); 
        } else if (subItem instanceof JSONArray) { 
         // JSON Array inside array 
        } else { 
         // Something 
        } 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

希望這有助於。

+0

感謝您的答案,但這隻會返回實際的json對象而不是其中的任何東西。 – user1136994 2013-05-13 16:58:53

+0

您可以遞歸地編寫此函數並從中獲取項目。 – 2013-05-13 17:42:10

+0

你能給我一個關於如何做aswin的小例子,謝謝 – user1136994 2013-05-14 05:46:32

相關問題