2016-08-25 136 views
0

訪問數組這是透過JSON.stringify()方法創建的JSON對象。 我無法訪問data1(數組在這裏)。如何訪問和打印它。如何從字符串化對象

{"data1":[{"text":"TMS-1","id":"1.0","children":[{"text":"Services","id":"1.0.0","children":[{"text":"child service","id":"1.0.0.0"}]},{"text":"Applications","id":"1.0.1","children":[]},{"text":"Radios","id":"1.0.2","children":[{"text":"radio child","id":"1.0.2.0","children":[{"text":"child 2","id":"1.0.2.0.0"},{"text":"child 2.1","id":"1.0.2.0.1"},{"text":"child 2.2","id":"1.0.2.0.2"}]},{"text":"radio service1","id":"1.0.2.1","children":[{"text":"child 1.2.1","id":"1.0.2.0.0"}]}]},{"text":"Platform","id":"1.0.3","children":[{"text":"platform child","id":"1.0.3.0"}]},{"text":"Faults","id":"1.0.4","children":[{"text":"Fault child","id":"1.0.4.0"}]}]}]} 
+0

裏姆更多https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify這個函數是輸出一個字符串,而不是數組 –

+0

是的。我們可以訪問字符串中的數組嗎? @ZayLau –

回答

0

嘗試

var str = JSON.stringify(something); // should be {"data1":[{"text":"TMS-1","id":"1.0","children":[{"text":"Services","id":"1.0.0","children":[{"text":"child service","id":"1.0.0.0"}]},{"text":"Applications","id":"1.0.1","children":[]},{"text":"Radios","id":"1.0.2","children":[{"text":"radio child","id":"1.0.2.0","children":[{"text":"child 2","id":"1.0.2.0.0"},{"text":"child 2.1","id":"1.0.2.0.1"},{"text":"child 2.2","id":"1.0.2.0.2"}]},{"text":"radio service1","id":"1.0.2.1","children":[{"text":"child 1.2.1","id":"1.0.2.0.0"}]}]},{"text":"Platform","id":"1.0.3","children":[{"text":"platform child","id":"1.0.3.0"}]},{"text":"Faults","id":"1.0.4","children":[{"text":"Fault child","id":"1.0.4.0"}]}]}]} 
var JsonArray = JSON.parse(str); 

閱讀更多:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

0

實現以下代碼來獲取數組。

try { 
     JSONObject jsonObject = new JSONObject(yourResponseString); 
     JSONArray jsonArray = jsonObject.getJSONArray("data1"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
相關問題