0

Here I have some JSON generated by Jenkins, I would like to access the value Started by user XXX in the below JSON with GSON.

Although as you can see there are two causes in the actions array. Although sometimes there is only one (In which case just return that one).

Question: How can I access the first causes only OR even better conjoin them both so I could access it like Started by user XXX - Rebuilds build #2

I cannot change the JSON unfortunately.

Edit* I understand the title is probably not the best although I cannot think of another way of phrasing it so if anyone can think of a better one I would be much obliged.

{
  "jobs" : [
    {
      "name" : "Test Build",
      "url" : "URL",
      "lastBuild" : {
        "actions" : [
          {
            "causes" : [
              {
                "shortDescription" : "Started by user XXX"
              }
            ]
          },
          {
            "causes" : [
              {
                "shortDescription" : "Rebuilds build #2"
              }
            ]
          },
          {
            "parameters" : [
              {
                "name" : "ENVIRONMENT",
                "value" : "PROD"
              },
              {
                "name" : "RELEASE"
              }
            ]
          }
        ],
        "building" : false,
        "duration" : 126580,
        "estimatedDuration" : 74509,
        "number" : 3,
        "timestamp" : 1445261252000,
        "url" : "URL",
        "builtOn" : "D-slave"
      }
    },
Jack
  • 2,891
  • 11
  • 48
  • 65

1 Answers1

1

Try with expression below. 0 index will give you first element.

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

This will give you

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

Change the expression wherever necessary.

Java code for all classes below - based on your json format.

So you need something like 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+"]";
    }
}
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
  • Sorry how is this supposed to be used with GSON? – Jack Dec 16 '15 at 15:24
  • Well for that you need to write code and get the data for a given pojo or object. Try reading these links http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java and http://www.javacreed.com/simple-gson-example/ to get started. At the end of day its JSONArray, JSONObject or JSONElement. – Sheetal Mohan Sharma Dec 16 '15 at 15:29
  • Yeah it's the GSON part I need help with. How would I set up the class to be able to accept both or just the first `causes` – Jack Dec 16 '15 at 15:30
  • Ah I see you can set it with class[] that's brilliant thank you! – Jack Dec 16 '15 at 17:23