简体   繁体   English

JSON用于构建多级树

[英]JSON for constructing multilevel tree

I wanted to form JSON like this: 我想像这样形成JSON:

{
    "Schedule": [
        {
            "id": "A",
            "name": "Summary",
            "ischild": "1",
            "level1": [
                {
                    "id": "A.1",
                    "name": "A.1",
                    "ischild": "1",
                    "level2": [
                        {
                            "id": "A.1.a",
                            "name": "Income Statement",
                            "ischild": "0"
                        },
                        {
                            "id": "A.1.b",
                            "name": "Balance Sheet",
                            "ischild": "0"
                        },
                        {
                            "id": "A.1.c",
                            "name": "A.1.c",
                            "ischild": "1",
                            "level3": [
                                {
                                    "id": "A.1.c.1",
                                    "name": "General RWA",
                                    "ischild": "0"
                                },
                                {
                                    "id": "A.1.c.2",
                                    "name": "Standardized RWA",
                                    "ischild": "0"
                                },
                                {
                                    "id": "A.1.c.3",
                                    "name": "Advanced RWA",
                                    "ischild": "0"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

But my code is giving below output: 但是我的代码给出以下输出:

{
    "Schedule": [
        {
            "name": "Summary",
            "ischild": "1",
            "id": "A",
            "N_LEVEL": "1"
        },
        {
            "name": "A.1",
            "ischild": "1",
            "id": "A.1",
            "N_LEVEL": "2"
        },
        {
            "name": "Income  Statement",
            "ischild": "0",
            "id": "A.1.a",
            "N_LEVEL": "3"
        },
        {
            "name": "Balance Sheet",
            "ischild": "0",
            "id": "A.1.b",
            "N_LEVEL": "3"
        },
        {
            "name": "A.1.c",
            "ischild": "1",
            "id": "A.1.c",
            "N_LEVEL": "3"
        },
        {
            "name": "General RWA",
            "ischild": "0",
            "id": "A.1.c.1",
            "N_LEVEL": "4"
        },
        {
            "name": "Standardized RWA",
            "ischild": "0",
            "id": "A.1.c.2",
            "N_LEVEL": "4"
        },
        {
            "name": "Advanced RWA",
            "ischild": "0",
            "id": "A.1.c.3",
            "N_LEVEL": "4"
        }
    ]
}

Here is my code: 这是我的代码:

public static String getJSONFromResultSet(ResultSet rs,String keyName) 
{
    System.out.println(" in getJSONFromResultSet method");
    Map json = new HashMap(); 
    List list = new ArrayList();
    if(rs!=null)
    {
        try
        {
            ResultSetMetaData metaData = rs.getMetaData();
            while(rs.next())
            {
                Map<String,Object> columnMap = new HashMap<String, Object>();
                for(int columnIndex=1;columnIndex<=metaData.getColumnCount();columnIndex++)
                {
                    if(rs.getString(metaData.getColumnName(columnIndex))!=null)
                            columnMap.put(metaData.getColumnLabel(columnIndex),rs.getString(metaData.getColumnName(columnIndex)));
                    else
                            columnMap.put(metaData.getColumnLabel(columnIndex), "");
                }
                list.add(columnMap);
            }
        } 
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        json.put(keyName, list);
    }
    return JSONValue.toJSONString(json);

I think your target structure could be better if it's names didn't change on every level. 我认为,如果目标名称在每个级别上都没有更改,那么您的目标结构可能会更好。 The level number is a value not a key . 级别号是一个而不是 ischild makes no sense either, I think this is isNotALeaf , well that can be worked out, so leave that off too, so we have: ischild也没有意义,我认为这是isNotALeaf ,可以解决,所以isNotALeaf ,所以我们有:

{
        "id": "A",
        "name": "Summary",
        "level": "1",
        "children": [
            {
                "id": "A.1",
                "name": "A.1",
                "level": "2",
                "children": [
                    {
                        "id": "A.1.a",
                        "name": "Income Statement",
                        "level": "3"
                    },
                    {
                        "id": "A.1.b",
                        "name": "Balance Sheet",
                        "level": "3"
                    }
                 ]
             }
}

Then generate a self-referencing class with based on that for use in GSon: 然后根据该类生成一个自引用类,以供GSon使用:

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Child {

    @Expose
    private String id;
    @Expose
    private String name;
    @Expose
    private String level;
    @Expose
    private List<Child> children = new ArrayList<Child>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public List<Child_> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM