String node for JSON : JSON « Development Class « Java






String node for JSON

/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */// geronimo import java.util.ArrayList; import java.util.Iterator; import java.util.Hashtable; import java.util.Collections; publicclass StringNode implements Comparable{ public String name = null; public ArrayList childs = new ArrayList(); public StringNode(String nm, ArrayList elements){ name = nm; childs = elements; } public StringNode(String nm){ name = nm; } public String getName(){ return name; } publicvoid setName(String nm){ name = nm;; } public ArrayList getChilds(){ return childs; } publicvoid setChilds(ArrayList elements){ childs = elements; } publicvoid addChild(Object obj){ childs.add(obj); } public StringNode findNode(String id){ if(id == null)return null; if(name != null && name.equals(id)) returnthis; Iterator iter = childs.iterator(); while(iter.hasNext()){ Object obj = iter.next(); if(obj instanceof StringNode){ StringNode tree = ((StringNode)obj).findNode(id); if(tree != null)return tree; } } return null; } publicboolean equals(Object node){ if(node instanceof StringNode && ((StringNode)node).name.equals(this.name)) return true; return false; } public String toJSONObject(String prependId){ return toJSONObject(prependId, null); } public String toJSONObject(String prependId, Hashtable htLinks){ return toJSONObject(prependId, htLinks, false); } public String toJSONObject(String prependId, Hashtable htLinks, boolean level1){ StringBuffer stb = new StringBuffer(); if(htLinks != null){ if(!name.startsWith("class ") && !name.startsWith("interface ") && !name.equals("Classes") && !name.equals("Interfaces") && htLinks.containsKey(name) && !level1){ stb.append("{title:'link::"); stb.append(htLinks.get(name)); stb.append("',widgetId:'"); stb.append(prependId); stb.append("'}"); return stb.toString(); } else { htLinks.put(name, prependId); } } stb.append("{title:'"); if(name != null) stb.append(name); stb.append("',widgetId:'"); stb.append(prependId); if(childs == null || childs.size() == 0){ stb.append("',children:[]}"); } else { stb.append("',children:["); Collections.sort(childs); for(int i=0;i<childs.size();i++){ Object obj = childs.get(i); if(i !=0 )stb.append(","); if(obj instanceof StringNode) stb.append(((StringNode)obj).toJSONObject(prependId+"."+i, htLinks)); else { stb.append("{title:'"); stb.append((String)obj); stb.append("',widgetId:'"); stb.append(prependId+"."+i); stb.append("'}"); } } stb.append("]}"); } return stb.toString(); } publicint compareTo(Object obj){ if(name == null) return -1; return name.compareTo(((StringNode)obj).getName()); } } 








Related examples in the same category

1.JSON Parser
2.Create Object from JSON string
3.Outputing data in JSON format in response to a JSONP request
4.JSON tokenizer.It makes tokens for parsing json data.
close