Java 如何将json值返回到另一个方法中
我正在从API获取JSON响应。 我应该将这些JSON响应返回给另一个方法Java 如何将json值返回到另一个方法中,java,arrays,json,object,Java,Arrays,Json,Object,我正在从API获取JSON响应。 我应该将这些JSON响应返回给另一个方法 HttpClient client = HttpClientBuilder.create().build(); HttpGet request=new HttpGet("/2.0/clusters/list"); request.addHeader("Authorization",bearerToken); request.addHeader("cache-control", "no-
HttpClient client = HttpClientBuilder.create().build();
HttpGet request=new HttpGet("/2.0/clusters/list");
request.addHeader("Authorization",bearerToken);
request.addHeader("cache-control", "no-cache");
HttpResponse response=client.execute(request);
System.out.println("Response Code:" +
response.getStatusLine().getStatusCode());
String json = EntityUtils.toString(response.getEntity());
System.out.println("Gather Details\n");
JSONObject cluster = new JSONObject(json);
JSONArray array=cluster.getJSONArray("clusters");
for (int i=0;i< array.length();i++)
{
JSONObject clusters = array.getJSONObject(i);
String id=clusters.get("id").toString();
String time=clusters.get("time").toString();
System.out.println("Id:"+id+"time:"+time+"\n");
if(response.getStatusLine().getStatusCode()!=200) {
System.out.println("Failed HTTP
response"+response.getStatusLine().getStatusCode()+" "+json);
}
return json;
/*Another method which takes json values and insert into db*/
public void insertdb(JSONObject json) throws Exception{
Connection con = ConnectToDB();
String tablename="Cluster_Info";
JSONObject cluster = new JSONObject(json);
System.out.println(cluster);
JSONArray array=cluster.getJSONArray("clusters");
HttpClient-client=HttpClientBuilder.create().build();
HttpGet请求=新的HttpGet(“/2.0/clusters/list”);
addHeader(“授权”,bearerToken);
addHeader(“缓存控制”、“无缓存”);
HttpResponse response=client.execute(请求);
System.out.println(“响应代码:”+
response.getStatusLine().getStatusCode());
字符串json=EntityUtils.toString(response.getEntity());
System.out.println(“收集详细信息\n”);
JSONObject集群=新的JSONObject(json);
JSONArray数组=cluster.getJSONArray(“集群”);
对于(int i=0;i
请帮助我将json响应发送给其他方法以插入数据库。- 使用gson-2.1.jar或更高版本
- 只需使用ID和时间字段(使用getter/setter)创建POJO(聚集)
- 添加另一个具有如下列表项的类
public class GatherDetails{private List<Gather> items;}
public void insertdb(GatherDetails gatherDetails) throws Exception{
for(Gather gather:GatherDetails.items){
gather.getId();gather.getTime();
//insert logic goes here
}}
只需将
insertdb
方法参数的类型更改为String
,然后在方法中将其解析为JSONObject
。
怎么了?你确定你的insertdb方法使用JSONObject作为参数吗?看起来它应该使用json字符串,因为它将输入参数转换为JSONObject。你可以在保存JSONObject的地方共享代码吗?如果你想对你的all API执行此操作,那么我建议你为是特定的任务。您可以将其用作API的预处理或后处理。
public void insertdb(GatherDetails gatherDetails) throws Exception{
for(Gather gather:GatherDetails.items){
gather.getId();gather.getTime();
//insert logic goes here
}}
/*Another method which takes json values and insert into db*/
public void insertdb(String jsonString) throws Exception{
// parse object
JSONObject json = JSONObject.parseObject(jsonString);
Connection con = ConnectToDB();
String tablename="Cluster_Info";
JSONObject cluster = new JSONObject(json);
System.out.println(cluster);
JSONArray array=cluster.getJSONArray("clusters");