0

I am struggling at transfering this code from JQuery to Java:

var url = 'http://gdata.youtube.com/feeds/api/videos/'; 

$.getJSON( url + '?v=2&alt=json-in-script&callback=?', {
    'q': q,
  }, function(data) {gotData(q, data);});

function gotData(q, data){
  for(var i in data.feed.entry){
     // print the data...
  }
}

It calls a YouTube API and reads some of the JSON result fields.

What is the easiest and most proper way to implement this in Java? Since I need it for just one API method call, I need the lightest possible solution. But I am guessing the code will become much more complex in Java, whatever library I used.

Also, I don't want to use YouTube API client for Java, but some general REST-JSON solution instead, which I could use for a few more API-s in future.

Ognjen
  • 2,508
  • 2
  • 31
  • 47

3 Answers3

1

I suggest Jersey-client for REST and json-simple for json parser.

please look http://code.google.com/p/json-simple/ for json parser and http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/ to create REEST Client.

kundan bora
  • 3,821
  • 2
  • 20
  • 29
1

You could use Apache HttpClient and Jackson Json:

DefaultHttpClient httpClient = new DefaultHttpClient();
// Add query string into get request
HttpGet getRequest = new HttpGet("http://gdata.youtube.com/feeds/api/videos/");

getRequest.setHeader("Accept", "application/json");

HttpResponse response = httpClient.execute(getRequest);

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = mapper.readValue(response.getEntity().getContent(), new TypeReference<HashMap<String,Object>>() {});
johnlcox
  • 520
  • 6
  • 12
0

I would use google-gson Google Gson
It annotation driven, very easy to use.

Community
  • 1
  • 1
Mr T.
  • 4,278
  • 9
  • 44
  • 61