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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>>() {});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.