无法使用JSON在数组下添加数组元素

时间:2015-07-20 14:09:22

标签: java json

我必须以下面的示例格式生成JSON:

    [
    {   "roleName" : "Parent Folder", "folderId" : "role1", "expanded" : true, 
        "children" : 
                    [                                                                                                     
                      { "roleName" : "subUser1 non-openable folder", "folderId" : "role11","fileicon" : true },
                      { "roleName" : "subUser2", "folderId" : "role12", "expanded" : true, 
            "children" : 
                       [        
                          { "roleName" : "subUser2-1", "folderId" : "role121", "expanded" : true, "children" : 
                           [
                            { "roleName" : "subUser2-1-1 folder ico", "folderId" : "role1211" },
                            { "roleName" : "subUser2-1-2 file ico", "folderId" : "role1212" , "fileicon" : true}
                           ]
                          }
                      ]
                      }
                    ]
    }
 ]

我为此创建了POJO,并且能够添加数组元素,但无法在元素内部或下方添加一个数组。请建议。

下面是我正在使用的pojo。

public class TargetFolder
{
    private TargetChildren[] children;

    private String roleName;

    private String expanded;

    private Long folderId;

    public TargetFolder(String roleName,
            String isFolder, Long folderId, TargetChildren[] folderList) {
        super();
        this.roleName = roleName;
        this.expanded = isFolder;
        this.folderId = folderId;
        this.children = folderList;
    }



    public TargetChildren[] getChildren ()
    {
        return children;
    }

    public void setChildren (TargetChildren[] children)
    {
        this.children = children;
    }

    public String getRoleName ()
    {
        return roleName;
    }

    public void setRoleName (String roleName)
    {
        this.roleName = roleName;
    }

    public String getExpanded ()
    {
        return expanded;
    }

    public void setExpanded (String expanded)
    {
        this.expanded = expanded;
    }

    public Long getFolderId ()
    {
        return folderId;
    }

    public void setFolderId (Long folderId)
    {
        this.folderId = folderId;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [children = "+children+", roleName = "+roleName+", expanded = "+expanded+", folderId = "+folderId+"]";
    }
}

and

public class TargetChildren
{


    private String fileicon;

    private String roleName;

    private long folderId;


    public TargetChildren(String roleName, String fileicon, long folderId) {
        super();
        this.fileicon = fileicon;
        this.roleName = roleName;
        this.folderId = folderId;
    }


    public String getFileicon ()
    {
        return fileicon;
    }

    public void setFileicon (String fileicon)
    {
        this.fileicon = fileicon;
    }

    public String getRoleName ()
    {
        return roleName;
    }

    public void setRoleName (String roleName)
    {
        this.roleName = roleName;
    }

    public long getFolderId ()
    {
        return folderId;
    }

    public void setFolderId (long folderId)
    {
        this.folderId = folderId;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [fileicon = "+fileicon+", roleName = "+roleName+", folderId = "+folderId+"]";
    }
}

以下是我用来生成JSON的逻辑:

for(int i  = 0; i<folderList.size();i++)
        {
            if(folderList!=null)
            {
                subList  = (List)folderList.get(i);
                childFolders[i] = new TargetChildren((String)subList.get(0),(String)subList.get(2),(Long)subList.get(1));
                JSONArray arr =  new JSONArray();
                if(((String)subList.get(2)).equals("true"))
                {
                    arr.put(i, childFolders[i]);
                }
                System.out.println(arr.toString());
                //TargetChildren [] testArr = new TargetChildren[] { new TargetChildren("Folder", "folderName", 226886843L)};
            }
        }
        TargetFolder targetFolder = new TargetFolder(parentFoldername,isFolder,folderId, childFolders);
        JSONObject jsonObject = new JSONObject(targetFolder);
        String jsonString = jsonObject.toString();
        System.out.println("JSON TO UI------ "+jsonString);

1 个答案:

答案 0 :(得分:1)

我能想到的最优雅,最简单的解决方案是为您的POJO添加toJSON方法,让它自己处理序列化。

对于TargetFolder

public JSONObject toJSON(){
    JSONObject out = new JSONObject();
    out.put("rolename", rolename);
    out.put("expanded", expanded);
    out.put("folderID", folderId);

    JSONArray children = new JSONArray();
    for(int i = 0; i < this.children.length; i++){
        children.push(this.children[i].toJSON());
    }
    out.put("children", children);

    return out;
}

TargetChildren执行相同的操作,然后通过调用:

将其转换为JSON
myTargetFolder.toJSON();

这样您就不必担心生成的JSON的递归结构。

如果添加一个带JSONObject的构造函数,则可以确保在一个位置进行一致的序列化和反序列化。

谷歌的GSON库也应该基本相同,但我从未使用它,所以我不能说它是如何工作的。

PS:您可能想为TargetFolderTargetChild创建一个公共超类,并将其用作children - 数组的数据类型,因为从JSON看起来就像这个数组可以包含TargetFolder的对象 - 属性(“展开”和“子”)和带有TargetChild的对象 - 属性(“fileicon”)