使用相同的命名父级访问值

时间:2015-12-16 15:07:03

标签: java json jenkins gson

这里我有一些Jenkins生成的JSON,我想通过GSON访问下面JSON中的值div { color: #00FF00; } /* h1 is red by default ... */ h1 { color: #FF0000 } /* ... but not when inside a div with class .title-in-green */ div.title-in-green h1 { color: #00FF00; }

虽然您可以看到Started by user XXX数组中有两个causes。虽然有时只有一个(在这种情况下只返回那个)。

问题:如何才能访问第一个actions,或者更好地将它们联合起来,以便我可以像causes

那样访问它们

不幸的是,我无法改变JSON。

编辑*我理解标题可能不是最好的,虽然我想不出另一种方式来表达它,所以如果有人能想到一个更好的,我会非常感激。

Started by user XXX - Rebuilds build #2

1 个答案:

答案 0 :(得分:1)

尝试下面的表达式。 0索引将为您提供第一个元素。

jobs[0].lastBuild.actions[0].causes

这会给你

[
   [
      {
         "shortDescription":"Started by user XXX"
      }
   ]
]

在必要时更改表达式。

以下所有类的Java代码 - 基于您的json格式。

所以你需要像jenkinjobs.jobs(0).getActions(0)这样的东西。

    public class Jenkinjobs
    {
        private Jobs[] jobs;

        public Jobs[] getJobs ()
        {
            return jobs;
        }

        public void setJobs (Jobs[] jobs)
        {
            this.jobs = jobs;
        }

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


public class Jobs
{
    private LastBuild lastBuild;

    private String name;

    private String url;

    public LastBuild getLastBuild ()
    {
        return lastBuild;
    }

    public void setLastBuild (LastBuild lastBuild)
    {
        this.lastBuild = lastBuild;
    }

    public String getName ()
    {
        return name;
    }

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

    public String getUrl ()
    {
        return url;
    }

    public void setUrl (String url)
    {
        this.url = url;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [lastBuild = "+lastBuild+", name = "+name+", url = "+url+"]";
    }
}


public class LastBuild
{
    private String timestamp;

    private String estimatedDuration;

    private String duration;

    private String building;

    private String number;

    private String builtOn;

    private String url;

    private Actions[] actions;

    public String getTimestamp ()
    {
        return timestamp;
    }

    public void setTimestamp (String timestamp)
    {
        this.timestamp = timestamp;
    }

    public String getEstimatedDuration ()
    {
        return estimatedDuration;
    }

    public void setEstimatedDuration (String estimatedDuration)
    {
        this.estimatedDuration = estimatedDuration;
    }

    public String getDuration ()
    {
        return duration;
    }

    public void setDuration (String duration)
    {
        this.duration = duration;
    }

    public String getBuilding ()
    {
        return building;
    }

    public void setBuilding (String building)
    {
        this.building = building;
    }

    public String getNumber ()
    {
        return number;
    }

    public void setNumber (String number)
    {
        this.number = number;
    }

    public String getBuiltOn ()
    {
        return builtOn;
    }

    public void setBuiltOn (String builtOn)
    {
        this.builtOn = builtOn;
    }

    public String getUrl ()
    {
        return url;
    }

    public void setUrl (String url)
    {
        this.url = url;
    }

    public Actions[] getActions ()
    {
        return actions;
    }

    public void setActions (Actions[] actions)
    {
        this.actions = actions;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [timestamp = "+timestamp+", estimatedDuration = "+estimatedDuration+", duration = "+duration+", building = "+building+", number = "+number+", builtOn = "+builtOn+", url = "+url+", actions = "+actions+"]";
    }
}

public class Parameters
{
    private String name;

    private String value;

    public String getName ()
    {
        return name;
    }

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

    public String getValue ()
    {
        return value;
    }

    public void setValue (String value)
    {
        this.value = value;
    }

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



public class Actions
{
    private Causes[] causes;

    public Causes[] getCauses ()
    {
        return causes;
    }

    public void setCauses (Causes[] causes)
    {
        this.causes = causes;
    }

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



public class Causes
{
    private String shortDescription;

    public String getShortDescription ()
    {
        return shortDescription;
    }

    public void setShortDescription (String shortDescription)
    {
        this.shortDescription = shortDescription;
    }

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