0

I have a javascript by which i call a rest api and get a JSON result , I need this result in a java class.

Below is the script

<script type="text/javascript">
function getCurrentTime()
{
var time;
     AJS.$.ajax({
        url: "/rest/vertygosla/1.0/compute/issue/TST-30",
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            time = data.sla;
        } 
     });
     return time[0].remainingTime;
}

alert(""+getCurrentTime())
</script>

json Provided by the server {"issueKey":"TST-30","issueId":11600,"created":"2013-01-31T11:38:38.765+0530","sla":[{"agreementName":"ack times","remainingTime":5860985,"endDate":"2013-01-31T12:00:57.796+0530","startDate":"2013-01-31T11:38:38.781+0530","consumedTime":1339015,"name":"Ack time","id":11000,"delayed":false,"closed":true,"frozen":false}]}

Can someone provide me a similar example in JAVA .

Thanks

Mizan
  • 450
  • 1
  • 5
  • 28
  • It would help if you provided the JSON which was returned by the server. –  Feb 01 '13 at 10:22
  • Check [Get the response of a HTTP GET request](http://stackoverflow.com/questions/8089189/get-the-response-of-a-http-get-request). It is very similar to yours. And answer show how to make a request and parse json. – Mikita Belahlazau Feb 01 '13 at 10:27

2 Answers2

1

If you want the json result in Java then you should use the JRJC which is a client specifically made for accessing JIRA REST API using Java. Otherwise you can send a HTTP Request in Java to get JSON from a url.

EDIT:You can send a HTTP request in Java or any other language. I prefer jquery or javascript since you don't have to implement the login procedure. If you are logged into jira, you can directly get the JSON results without implementing login. JRJC client is really slow.

Following is a way of getting JSON from JIRA in jquery:

$.getJSON('https://jira.atlassian.com/rest/api/latest/search?jql=assignee=xyz&expand&jsonp-callback=?', function(data) {
    console.log(data);
});
});
dejavu
  • 3,236
  • 7
  • 35
  • 60
0

You can use RestTemplate in java to make a Rest call and then use JSONObject to retrieve the JSON results as java object.

Below is an example:

RestTemplate restTemplate = new RestTemplate();
try {
    String jsonResult = restTemplate.getForObject(url, String.class);
    JSONObject jsonObject = (JSONObject)new JSONParser().parse(jsonResult);
} catch (Exception e) {
    e.printStackTrace();
}
zdesam
  • 2,936
  • 3
  • 25
  • 32
  • You'd better post links to the libs you're using in your code. – Mikita Belahlazau Feb 01 '13 at 10:36
  • thanks , can you also provide me the imports for the above code .. ? I get unable to resolve exceptions :) – Mizan Feb 01 '13 at 10:36
  • you need spring web client library for RestTemplate and Java Json library for JSONObject. You must be able to download them from springsource.org and json.org site **https://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.0.RELEASE/spring-framework-3.2.0.RELEASE-dist.zip** – zdesam Feb 01 '13 at 10:44
  • Jars required to JSON in java program.. json-lib-2.2.2-jdk15.jar ezmorph.jar commons-lang.jar commons-logging.jar commons-beanutils.jar commons-collections.jar – zdesam Feb 01 '13 at 10:51
  • @zdesam I will try this and let you know how it goes , thanks :) – Mizan Feb 01 '13 at 11:03
  • @zdesam it says cannot resolve JSONParser , I guess i cant use spring ... cant this be done using default java libs ? – Mizan Feb 01 '13 at 11:40