Json to json Tree Structure

Source: Internet
Author: User

Json to json Tree Structure

When writing a Restful service, because all returned objects are json objects, these objects are related, but they are not well reflected. when returning to the page, cannot be a tree. The returned json string data is as follows:

[{Belongsname:, id: 901, isleaf: 0, name: XJBHX-2 standard Project Department, pid:, type :}, {belongsname:, id: 902, isleaf: 1, name: general Department (office), pid: 901, type: },{ belongsname:, id: 903, isleaf: 1, name: Engineering Department (Technical Department/Technology Department), pid: 901, type :}, {belongsname:, id: 904, isleaf: 1, name: security, pid: 901, type :}, {belongsname:, id: 905, isleaf: 1, name: Financial Department, pid: 901, type: },{ belongsname:, id: 906, isleaf: 1, name: Department of thing (Department of thing), pid: 901, type :}, {belongsname:, id: 907, isleaf: 1, name: Central Laboratory, pid: 901, type:}]

The data we need on the front-end interface may be as follows:

[
{
Belongsname :,
Id: 901,
Isleaf: 0,
Name: XJBHX-2 Project Department,
Pid :,
Type:
Children :[{
Belongsname :,
Id: 902,
Isleaf: 1,
Name: General Department (office ),
Pid: 901,
Type:
},
{
Belongsname :,
Id: 903,
Isleaf: 1,
Name: Engineering Department (Engineering and Technology Department/Technology Department ),
Pid: 901,
Type:
},
{
Belongsname :,
Id: 904,
Isleaf: 1,
Name: Security Department,
Pid: 901,
Type:
},
{
Belongsname :,
Id: 905,
Isleaf: 1,
Name: Ministry of Finance,
Pid: 901,
Type:
},
{
Belongsname :,
Id: 906,
Isleaf: 1,
Name: Department of thing (Department of thing and machine ),
Pid: 901,
Type:
},
{
Belongsname :,
Id: 907,
Isleaf: 1,
Name: Central Laboratory,
Pid: 901,
Type:
}]
]

How to convert it? Example:

 

<%@ page language=java contentType=text/html; charset=ISO-8859-1pageEncoding=ISO-8859-1%>
<Script type = text/javascript> // converts a json string to a function transData (a, idStr, pidStr, chindrenStr) {var r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, I = 0, j = 0, len =. length; for (; I <len; I ++) {hash [a [I] [id] = a [I] ;}for (; j <len; j ++) {var aVal = a [j], hashVP = hash [aVal [pid]; if (hashVP ){! HashVP [children] & (hashVP [children] = []); hashVP [children]. push (aVal);} else {r. push (aVal) ;}} return r ;}// test method function test () {// json string var jsonData = eval ('[{belongsname:, id: 901, isleaf: 0, name: XJBHX-2 standard Project Department, pid:, type: },{ belongsname:, id: 902, isleaf: 1, name: General Department (office), pid: 901, type :}, {belongsname:, id: 903, isleaf: 1, name: Engineering Department (Department of Technology/technology), pid: 901, type :}, {belongsname:, id: 904, isleaf: 1, name: Security Department, pid: 901, type :},{ belongsname:, id: 905, isleaf: 1, name: Ministry of Finance, pid: 901, type :}, {belongsname:, id: 906, isleaf: 1, name: TSL (TSL), pid: 901, type :}, {belongsname:, id: 907, isleaf: 1, name: Central Laboratory, pid: 901, type:}] '); // The bound field var jsonDataTree = transData (jsonData, 'id ', 'pid', 'chinance'); console. log (jsonDataTree);} // method window for loading and executing the form. onload = test; </script>

 

 

Summary:

At the beginning, I thought it was complicated. I thought I would traverse it and manually spell the json string. The network would always do what we did first.
 

 

Related Article

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.