如何在Java中使用XStream处理JSON?
我创建了一个谷歌定制搜索引擎,并使用HTTP GET启动了查询。现在谷歌正在以JSON格式返回结果。我只是想知道如何将这个JSON输出格式化为人类可读的方式 例如:如何在Java中使用XStream处理JSON?,java,xstream,google-custom-search,Java,Xstream,Google Custom Search,我创建了一个谷歌定制搜索引擎,并使用HTTP GET启动了查询。现在谷歌正在以JSON格式返回结果。我只是想知道如何将这个JSON输出格式化为人类可读的方式 例如: Title: The matrix htmlTitle: "The matrix.." 我已经在许多论坛上看到了XStream的推荐。但我不知道如何才能让它发挥作用 谁能帮我一下吗 仅供参考,我在这里给出了HTTP GET代码: public static void main(String[] args) throws IOExc
Title: The matrix
htmlTitle: "The matrix.."
我已经在许多论坛上看到了XStream的推荐。但我不知道如何才能让它发挥作用
谁能帮我一下吗
仅供参考,我在这里给出了HTTP GET代码:
public static void main(String[] args) throws IOException {
HttpGet httpget = new HttpGet("https://www.googleapis.com/customsearch/v1?key=AIzaSyBp_5Upf6h0QSXR8UveLs4_c6lAmGW_7B8&cx=014783642332862910131:opc1zgsvfhi&q=matrix&alt=json");
System.out.println(httpget.getURI());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpClient httpClient = new DefaultHttpClient();
String responseBody = httpClient.execute(httpget, responseHandler);
System.out.println(responseBody);
publicstaticvoidmain(字符串[]args)引发IOException{
HttpGet HttpGet=新的HttpGet(“https://www.googleapis.com/customsearch/v1?key=AIzaSyBp_5Upf6h0QSXR8UveLs4_c6lAmGW_7B8&cx=014783642332862910131:opc1zgsvfhi&q=matrix&alt=json");
System.out.println(httpget.getURI());
ResponseHandler ResponseHandler=新BasicResponseHandler();
HttpClient HttpClient=新的DefaultHttpClient();
字符串responseBody=httpClient.execute(httpget,responseHandler);
系统输出打印LN(响应库);
首先,您需要创建一个映射Goolge响应的类,然后使用如下代码(来自):
我不知道它是否与您想做的完全相同,但谷歌提供了一个Java库。可能是它使用起来很简单:
String json = "{\"product\":{\"name\":\"Banana\",\"id\":\"123\""
+ ",\"price\":\"23.0\"}}";
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("product", Product.class);
Product product = (Product)xstream.fromXML(json);
System.out.println(product.getName());