Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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中操作HTTP json响应数据_Java_Json_Http_Httpresponse - Fatal编程技术网

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

如何在Java中操作HTTP json响应数据,java,json,http,httpresponse,Java,Json,Http,Httpresponse,以json格式输出如下 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)); 我想知道的是,

以json格式输出如下

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));
我想知道的是,如何使用Java操作这些数据,而不将其写入文件? 我想打印代码中的名称、值

杰克逊图书馆是首选,但任何都可以


提前感谢

这里有两种不用图书馆的方法

新(更好)答案:

findInLine
可能会更好。(
scannerName.findInLine(模式);

可能是这样的:

----------------------------------------
{"session":{"name":"JSESSIONID","value":"5F736EF0A08ACFD7020E482B89910589"},"loginInfo":{"loginCount":50,"previousLoginTime":"2014-11-29T14:54:10.424+0530"}}
----------------------------------------
Scanner s = new Scanner(input).useDelimiter("\"");
w匹配单词字符(字母、数字和下划线),d匹配数字,并且+使其匹配多次(因此它不会在一个字符后停止)

在这里阅读有关模式的内容

旧答案:

我很确定你可以在这里使用带有自定义分隔符的扫描仪

s.findInLine("{"session":{"name":"(\\w+)","value":"(\\w+)"},"loginInfo":{"loginCount":(\\d+),"previousLoginTime":"(\\w+)"}}");
应该返回如下内容:

----------------------------------------
{"session":{"name":"JSESSIONID","value":"5F736EF0A08ACFD7020E482B89910589"},"loginInfo":{"loginCount":50,"previousLoginTime":"2014-11-29T14:54:10.424+0530"}}
----------------------------------------
Scanner s = new Scanner(input).useDelimiter("\"");
等等。然后只需对列表进行排序/使用更智能的分隔符/删除不必要的位。 扔掉所有其他物品是一个不错的开始

您可以使用库将json字符串解析为JSONObject,并从该对象读取值,如下所示:

{
session
:{
name
:
JSESSIONID
,
value
:
5F736EF0A08ACFD7020E482B89910589
您需要从要读取值的位置读取该对象。这里需要
name
参数的值,该参数位于
session
对象内,因此首先使用如上所示的函数获取
session
作为JSONObject的值,并从该对象读取
name

希望这能对您有所帮助。

我强烈建议使用基于apache http api的构建

JSONObject json = new JSONObject(EntityUtils.toString(entity));
JSONObject sessionObj =  json.getJSONObject("session");
System.out.println(sessionObj.getString("name"));
private static final HttpRequest HTTP_REQUEST=HttpRequestBuilder.createGet(yourUri,new TypeReference{})
.addDefaultHeaders(标题)
.build();
公共无效发送(){
ResponseHandler ResponseHandler=HTTP_REQUEST.execute();
Map data=responseHandler.get();
}
如果您想使用,您可以:

entity=httpResponse.getEntity();
ObjectMapper mapper=新的ObjectMapper();
Map data=mapper.readValue(entity.getContent(),新类型引用{});

如果您喜欢Jackson,为什么不创建bean类并使用ObjectMapper将其反序列化到bean?