如何从字符串化对象

访问数组这是透过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"}]}]}]} 

回答:

尝试

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

回答:

实现以下代码来获取数组。

try { 

JSONObject jsonObject = new JSONObject(yourResponseString);

JSONArray jsonArray = jsonObject.getJSONArray("data1");

} catch (JSONException e) {

e.printStackTrace();

}

以上是 如何从字符串化对象 的全部内容, 来源链接: utcz.com/qa/267171.html

回到顶部