简体   繁体   English

如何在Java中操作HTTP json响应数据

[英]how to manipulate HTTP json response data in Java

HttpGet getRequest=new HttpGet("/rest/auth/1/session/");
getRequest.setHeaders(headers);
httpResponse = httpclient.execute(target,getRequest);

entity = httpResponse.getEntity();

System.out.println(EntityUtils.toString(entity));

Output as follows in json format 以json格式输出如下

----------------------------------------
{"session":{"name":"JSESSIONID","value":"5F736EF0A08ACFD7020E482B89910589"},"loginInfo":{"loginCount":50,"previousLoginTime":"2014-11-29T14:54:10.424+0530"}}
----------------------------------------

What I want to know is how to you can manipulate this data using Java without writing it to a file? 我想知道的是如何不用Java将数据写入文件就可以操纵它? I want to print name, value in my code 我想在代码中打印名称,值

Jackson library is preferred but any would do. 杰克逊图书馆是首选,但任何人都可以。

thanks in advance 提前致谢

Here's two ways to do it without a library. 这是没有库的两种方法。

NEW (better) Answer: 新(更好)答案:

findInLine might work even better. findInLine可能会更好。 ( scannerName.findInLine(pattern); ) scannerName.findInLine(pattern);

Maybe something like: 也许像这样:

s.findInLine("{"session":{"name":"(\\w+)","value":"(\\w+)"},"loginInfo":{"loginCount":(\\d+),"previousLoginTime":"(\\w+)"}}");

w matches word characters (letters, digits, and underscore), d matches digits, and the + makes it match more than once (so it doesnt stop after just one character). w匹配单词字符(字母,数字和下划线),d匹配数字,而+使其匹配多次(因此仅在一个字符后不会停止)。

Read about patterns here https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html 在此处阅读有关模式的信息https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

OLD Answer: 老答案:

I'm pretty sure you could use a scanner with a custom delimiter here. 我很确定您可以在此处使用带有自定义定界符的扫描仪。

Scanner s = new Scanner(input).useDelimiter("\"");

Should return something like: 应该返回类似:

{
session
:{
name
:
JSESSIONID
,
value
:
5F736EF0A08ACFD7020E482B89910589

And so on. 等等。 Then just sort through that list/use a smarter delimiter/remove the unnecessary bits. 然后只需对列表进行排序/使用更智能的定界符/删除不必要的位即可。 Getting rid of every other item is a pretty decent start. 摆脱其他所有项目都是一个不错的开始。

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html has info on this. https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html包含有关此信息。

You may use this JSON library to parse your json string into JSONObject and read value from that object as show below : 您可以使用此JSON库将json字符串解析为JSONObject并从该对象读取值,如下所示:

JSONObject json = new JSONObject(EntityUtils.toString(entity));
JSONObject sessionObj =  json.getJSONObject("session");
System.out.println(sessionObj.getString("name"));

You need to read upto that object from where you want to read value. 您需要从要读取值的位置读取该对象。 Here you want the value of name parameter which is inside that session object, so you first get the value of session as JSONObject using getJSONObject(KeyString) and read name value from that object using function getString(KeyString) as show above. 在这里,你想要的值name参数,它是内部session对象,所以你第一次得到的值session使用作为JSONObject的getJSONObject(KeyString中)和读取name使用函数对象价值的getString(KeyString中)如上图所示。

May this will help you. 愿这对您有帮助。

I higly recomend http-request built on apache http api. 我高高兴兴地建议基于apache http api的http请求

private static final HttpRequest<Map<String, Map<String, String>>> HTTP_REQUEST = HttpRequestBuilder.createGet(yourUri, new TypeReference<Map<String, Map<String, String>>>{})
    .addDefaultHeaders(headers)
    .build();

public void send(){
   ResponseHandler<Map<String, Map<String, String>>> responseHandler = HTTP_REQUEST.execute();
   Map<String, Map<String, String>> data = responseHandler.get();
}

If you want use jackson you can: 如果要使用杰克逊 ,则可以:

entity = httpResponse.getEntity();
ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, String>> data = mapper.readValue(entity.getContent(),  new TypeReference<Map<String, Map<String, String>>>{});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM