2016-08-25 226 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(); 
    } 
相关问题