Conversion of Java collections into strings in JSON format

Source: Internet
Author: User

Currently, N-front-end technologies on the market display tree structures, the simplest of which is dtree. However, JS small frameworks such as jquery ext dhtmlx are slightly better than JS small frameworks for complicated implementation of trees with various controls, these frameworks support JSON data as the data source of the tree without exception. JSON is a good thing. For more information, see this website.
JSON introduces the help of developers on each platform. I was recently sent to the front-end for development and made an authorized menu authorization function, which is very common. Therefore, I wrote a tool class to convert a collection class with parent-child relationship to a JSON string for front-end display. It is not optimized or reconstructed. You need to modify it on your own.

Package COM. **. util;/*** converts a database record with a tree structure into JSON format data for foreground display as a tree using * @ author youjianbo 2011-08-04 youjianbo_han_87@hotmail.com **/import Java. util. arraylist; import Java. util. iterator; import Java. util. list; public class jsonutils {stringbuffer JSON = new stringbuffer (); List templist = new arraylist (); /*** construct a JSON file ** @ Param list * @ Param node */private string constructorjson (list, treenode tree Node) {If (haschild (list, treenode) {JSON. append ("{\" ID \ ": \" "); JSON. append (treenode. getnodeid () + "\" "); JSON. append (", \" text \ ": \" "); JSON. append (treenode. getnodename () + "\" "); If (treenode. getnodecode (). length () <2) {JSON. append (", \" state \ ": \" opened \ "");} else {JSON. append (", \" state \ ": \" Closed \ "");} If ("0 ". equals (treenode. getnodeid () {JSON. append (", \" iconcls \ ": \" icon-monitor \"");} Else {JSON. append (", \" iconcls \ ": \" icon-phonebook \ "");} JSON. append (", \" Children \ ": ["); templist. add (treenode); List childlist = getchildlist (list, treenode); iterator = childlist. iterator (); While (iterator. hasnext () {treenode node = (treenode) iterator. next (); constructorjson (list, node);} JSON. append ("]},");} else {JSON. append ("{\" ID \ ": \" "); JSON. append (treenode. getnodeid () + "\" "); JSON. append (", \" text \ ": \" "); JSON. append (treenode. getnodename () + "\" "); JSON. append ("},"); templist. add (treenode);} return JSON. tostring ();}/*** obtain subnode list information ** @ Param list * @ Param node * @ return */private list getchildlist (list, treenode node) {// obtain the subnode list li = new arraylist (); iterator it = List. iterator (); While (it. hasnext () {treenode n = (treenode) it. next (); If (N. getfat Her_nodeid (). Equals (node. getnodeid ())&&! N. getfather_nodeid (). equals (N. getnodeid () {Li. add (n) ;}} return Li ;}/ *** determine whether a subnode exists ** @ Param list * @ Param node * @ return */private Boolean haschild (list, treenode node) {return getchildlist (list, node ). size ()> 0? True: false;}/*** get the JSON format string of a single node and Its subnodes **/Public String getjson (list, treenode node) {JSON = new stringbuffer (); constructorjson (list, node); string jsondate = JSON. tostring (); Return jsondate. replaceall (",]", "]");}/*** get the JSON format string of multiple nodes and Their subnodes **/Public String getjson (list List) {templist = new arraylist (); JSON = new stringbuffer (); iterator = List. iterator (); While (ITER Ator. hasnext () {treenode node = (treenode) iterator. Next (); If (! Templist. contains (node) {constructorjson (list, node) ;}} string jsondate = JSON. tostring (); Return ("[" + jsondate + "]"). replaceall (",]", "]") ;}}

When I used jquery easy UI to display the tree, I found a bug. Post address: http://topic.csdn.net/u/20110822/14/9afd5459-6108-48af-bc54-e0365d9f824f.html

Example: http://download.csdn.net/source/3541195. I am not familiar with front-end development. I don't have time to take a closer look at jquery. If you have time, please help me. If the problem is solved, let's say, thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.