JSON到Java POJO类无法提取所有详细信息

时间:2018-01-06 23:21:16

标签: java json pojo

我正在使用jackson解析器来解析json。我想提取"道具"从JSON下面。请建议java POJO课程需要进行哪些更改才能获得"道具"值。下面是json和等效的POJO类。

JSON -

{
    "iActs": [
        {
            "iAct": "ref",
            "refConfigs": {
                "responseTypes": [
                    "COMPLETE",
                    "INCOMPLETE"
                ]
            },
            "sets": [
                {
                    "id": 2,
                    "set": "APROFILE",
                    "attrs": [
                        {
                            "id": 1,
                            "attr": "APROFILE.FAN",
                            "configs": {
                                "iResp": true
                            }
                        },
                        {
                            "id": 2,
                            "attr": "APROFILE.BTC",
                            "configs": {
                                "iResp": true
                            }
                        },
                        {
                            "id": 3,
                            "attr": "APROFILE.HNAME",
                            "configs": {
                                "iResp": true
                            }
                        },
                        {
                            "id": 4,
                            "attr": "APROFILE.HDETAILS",
                            "props": [
                                "Delhi",
                                "Mumbai",
                                "Kolkata",
                                "Bangalore",
                                "Jalandhar"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

JAVA POJO -

IActs.java

public class IActs
{
    private RefConfigs refConfigs;

    private String iAct;

    private Sets[] sets;

    public RefConfigs getRefConfigs ()
    {
        return refConfigs;
    }

    public void setRefConfigs (RefConfigs refConfigs)
    {
        this.refConfigs = refConfigs;
    }

    public String getIAct ()
    {
        return iAct;
    }

    public void setIAct (String iAct)
    {
        this.iAct = iAct;
    }

    public Sets[] getSets ()
    {
        return sets;
    }

    public void setSets (Sets[] sets)
    {
        this.sets = sets;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [refConfigs = "+refConfigs+", iAct = "+iAct+", sets = "+sets+"]";
    }
}

Sets.java

public class Sets
{
    private String id;

    private Attrs[] attrs;

    private String set;

    public String getId ()
    {
        return id;
    }

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

    public Attrs[] getAttrs ()
    {
        return attrs;
    }

    public void setAttrs (Attrs[] attrs)
    {
        this.attrs = attrs;
    }

    public String getSet ()
    {
        return set;
    }

    public void setSet (String set)
    {
        this.set = set;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", attrs = "+attrs+", set = "+set+"]";
    }
}

Attrs.java

public class Attrs
{
    private Configs configs;

    private String id;

    private String attr;

    public Configs getConfigs ()
    {
        return configs;
    }

    public void setConfigs (Configs configs)
    {
        this.configs = configs;
    }

    public String getId ()
    {
        return id;
    }

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

    public String getAttr ()
    {
        return attr;
    }

    public void setAttr (String attr)
    {
        this.attr = attr;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [configs = "+configs+", id = "+id+", attr = "+attr+"]";
    }
}   

Configs.java

public class Configs
{
    private String iResp;

    public String getIResp ()
    {
        return iResp;
    }

    public void setIResp (String iResp)
    {
        this.iResp = iResp;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [iResp = "+iResp+"]";
    }
}

RefConfigs.java

public class RefConfigs
{
    private String[] responseTypes;

    public String[] getResponseTypes ()
    {
        return responseTypes;
    }

    public void setResponseTypes (String[] responseTypes)
    {
        this.responseTypes = responseTypes;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [responseTypes = "+responseTypes+"]";
    }
}

我尝试了几种方法,但没有一种方法可行。提前谢谢。

2 个答案:

答案 0 :(得分:0)

你的Set类应该有一个Attrs对象实例,而不是一个数组Attrs []。您需要更改Attrs以获得对象Attr的集合(例如)。在那个对象中你应该定义属性:id,attr,configs和props。您还需要创建一个新的Props对象。

答案 1 :(得分:0)

只需将以下内容添加到您的Attrs课程中:

private String [] props;
public String[] getProps() {
    return props;
}
public void setProps(String[] props) {
    this.props = props;
}