how to manipulate HTTP json response data in Java

a 夏天 提交于 2020-01-04 12:48:30

问题


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

----------------------------------------
{"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? I want to print name, value in my code

Jackson library is preferred but any would do.

thanks in advance


回答1:


You may use this JSON library to parse your json string into JSONObject and read value from that object as show below :

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.

May this will help you.




回答2:


Here's two ways to do it without a library.

NEW (better) Answer:

findInLine might work even better. (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).

Read about patterns here 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.




回答3:


I higly recomend http-request built on apache http api.

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


来源:https://stackoverflow.com/questions/27201201/how-to-manipulate-http-json-response-data-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!