Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中实现RESTAPI客户端和JSON解析器的最简单方法是什么?_Java_Jquery_Json_Rest_Rest Client - Fatal编程技术网

在Java中实现RESTAPI客户端和JSON解析器的最简单方法是什么?

在Java中实现RESTAPI客户端和JSON解析器的最简单方法是什么?,java,jquery,json,rest,rest-client,Java,Jquery,Json,Rest,Rest Client,我正在努力将这段代码从JQuery转换为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

我正在努力将这段代码从JQuery转换为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...
  }
}
它调用YouTube API并读取一些JSON结果字段

用Java实现这一点最简单、最合适的方法是什么?因为我只需要为一个API方法调用使用它,所以我需要最简单的解决方案。但我猜想,无论我使用什么库,Java中的代码都会变得更加复杂

另外,我不想使用YouTube API client for Java,而是使用一些通用的REST-JSON解决方案,将来我可以将其用于更多的API-s

它由注释驱动,非常易于使用。

我建议Jersey client用于REST,json simple用于json解析器

请查找json解析器并创建REEST客户端。

您可以使用和:

DefaultHttpClient-httpClient=newdefaulthttpclient();
//将查询字符串添加到get请求中
HttpGet getRequest=新的HttpGet(“http://gdata.youtube.com/feeds/api/videos/");
setHeader(“接受”、“应用程序/json”);
HttpResponse response=httpClient.execute(getRequest);
ObjectMapper mapper=新的ObjectMapper();
Map jsonMap=mapper.readValue(response.getEntity().getContent(),new-TypeReference(){});
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>>() {});