当元素可以被不同类型使用时,如何创建JSON数据结构? [英] How do I create JSON data structure when element can be different types in for use by

查看:74
本文介绍了当元素可以被不同类型使用时,如何创建JSON数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

question ,我们发现children 元素可以是一个附加数据项的数组或者是一个布尔元素



我正在使用Java创建数据,存储在Data类中,然后使用Google Gson转换为Json。但是因为孩子可以是两种不同的东西,所以我通过在我的数据中使用变量 kids 来表示这种情况,当我需要将其存储为布尔值时使用它。然后对生成的json执行String替换以将其转换为子元素。



但是这种破解并不是非常令人满意和有问题的(即,如果他们有内容的真实数据孩子们),那么使用不同的数据类型表示数据的正确方式是什么?

  public class Data 
{
私人字符串ID;
私人字符串文本;
私人字符串图标;
私人州立州;
私人布尔孩子;

私人列表< Data> children = null;

public String getId()
{
return id;
}

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

public String getText()
{
return text;
}

public void setText(String text)
{
this.text = text;
}

public String getIcon()
{
return icon;
}

public void setIcon(String icon)
{
this.icon = icon;
}

public List< Data> getChildren()
{
返回儿童;
}

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

public State getState()
{
return state;
}

public void setState(State state)
{
this.state = state;
}

public boolean getKids()
{
return kids;
}

public void setKids(boolean kids)
{
this.kids = kids;



public static String createFolderJsonData()
{
CreateFolderTree cft = new CreateFolderTree(null);
String treeData = cft.start(1).replace(kids,children);
返回treeData;


解决方案

假设您知道什么类型孩子是你可以像这样解决

  public class Data< T> {

private T children = null;

public T getChildren(){
return children;
}

public void setChildren(T children){
this.children = children;



public class DataRunner {
public static void main(String [] args){
Data< List< Data>> data = new Data<>();

数据<列表<数据>> subDataOne = new Data<>();
subDataOne.setChildren(new ArrayList<>());

数据<布尔> subDataTwo = new Data<>();
subDataTwo.setChildren(true);

列表< Data> listData = new ArrayList<>();

listData.add(subDataOne);
listData.add(subDataTwo);

data.setChildren(listData);

// {children:[{children:[]},{children:true}]}
System.out.println(new Gson()。toJson (数据));






现在它打印正确的东西,注意 ArrayList 不在乎泛型类型 Data


In this question we see that children element can either be an array of additional data items or a boolean

I am using Java to create the data, store in a Data class and then convert to Json using Google Gson. But because children can be two different things I have represented this by having a variable kids in my Data that I use when I need to store as boolean. Then do a String replace on the resulting json to turn it into a children element.

But this hack is not very satisfactory and problematic (i.e if they have real data with content "kids") so what is the proper way to represent data with varying data types.

public class Data
{
    private String id;
    private String text;
    private String icon;
    private State  state;
    private boolean kids;

    private List<Data> children = null;

    public String getId()
    {
        return id;
    }

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

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
    }

    public String getIcon()
    {
        return icon;
    }

    public void setIcon(String icon)
    {
        this.icon = icon;
    }

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

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

    public State getState()
    {
        return state;
    }

    public void setState(State state)
    {
        this.state = state;
    }

    public boolean getKids()
    {
        return kids;
    }

    public void setKids(boolean kids)
    {
        this.kids = kids;
    }
}

public static String createFolderJsonData()
{
  CreateFolderTree cft = new CreateFolderTree(null);
  String treeData = cft.start(1).replace("kids", "children");
  return treeData;
}

解决方案

Assuming that you know what type the child is you can solve it like this

public class Data<T>{

    private T children = null;

    public T getChildren(){
        return children;
    }

    public void setChildren(T children){
        this.children = children;
    }
}

public class DataRunner {
    public static void main(String[] args){
        Data<List<Data>> data = new Data<>();

        Data<List<Data>> subDataOne = new Data<>();
        subDataOne.setChildren(new ArrayList<>());

        Data<Boolean> subDataTwo = new Data<>();
        subDataTwo.setChildren(true);

        List<Data> listData = new ArrayList<>();

        listData.add(subDataOne);
        listData.add(subDataTwo);

        data.setChildren(listData);

        // {"children":[{"children":[]},{"children":true}]}
        System.out.println(new Gson().toJson(data));
    }
}

Now it prints the correct thing, notice that the ArrayList doesn't care what generic type Data has

这篇关于当元素可以被不同类型使用时,如何创建JSON数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆