/** Topic hierarchy parsing */ public static TopicNode parseTopics(String json) throws JSONException { JSONObject jTopics = new JSONObject(json); ArrayList<TopicNode> topics = parseTopicChildren(jTopics); TopicNode root = new TopicNode(); root.setChildren(topics); return root; }
/** Reads through the given JSONObject and adds any topics to the given topic. */ private static ArrayList<TopicNode> parseTopicChildren(JSONObject jTopic) throws JSONException { // Don't allocate any lists if it's empty. if (jTopic.length() == 0) { return null; } @SuppressWarnings("unchecked") Iterator<String> iterator = jTopic.keys(); String topicName; ArrayList<TopicNode> topics = new ArrayList<TopicNode>(); TopicNode currentTopic; while (iterator.hasNext()) { topicName = iterator.next(); currentTopic = new TopicNode(topicName); currentTopic.setChildren(parseTopicChildren(jTopic.getJSONObject(topicName))); topics.add(currentTopic); } return topics; }