如何从Java中的HTTP请求中获取JSON对象 [英] How to get JSON object from HTTP request in Java

查看:745
本文介绍了如何从Java中的HTTP请求中获取JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正试图在Java线路中使用HTTP请求来获取JSON对象。

I'm now trying to get JSON Object with using HTTP request in Java cord.

我想知道如何在下面的电话中获得响应或JSON对象。

I want to know how I can get response or JSON object in the following cord.

请告诉我。

(在这个程序中,我试图获得维基百科类别的文章纽约。)

(In this program, I try to get Wikipedia categories of the article "New York". )

 String requestURL = "http://en.wikipedia.org/w/api.php?action=query&prop=categories&format=json&clshow=!hidden&cllimit=10&titles=" + words[i];
 URL wikiRequest = new URL(requestURL);
 URLConnection connection = wikiRequest.openConnection();  
 connection.setDoOutput(true);  

                    /**** I'd like to get response here. ****/

 JSONObject json = Util.parseJson(response);


推荐答案

Scanner scanner = new Scanner(wikiRequest.openStream());
String response = scanner.useDelimiter("\\Z").next();
JSONObject json = Util.parseJson(response);
scanner.close();

这篇关于如何从Java中的HTTP请求中获取JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆