读取API响应的通用代码

时间:2018-08-22 05:40:39

标签: java json

我需要通用代码才能读取响应。 在下面的响应中,我要打印所有jsonobjects(响应标头和response)以及这些对象中存在的所有json数组,并在这些数组中包含所有字符串或子节点。下面仅是一个示例,我想使其通用,以便在命中任何API时将获取其各自的json对象,数组和值。

响应

{"responseHeader":{"status":0,"QTime":0,"params":{"q":"banga","df":null,"fl":"ID,Name,rank,cc,geoloc","json":"","sort":"rank desc,score desc","fq":"cc:IND OR cc:NPL","wt":"json"}},"response":{"numFound":10,"start":0,"docs":[{"ID":122,"Name":"Bangalore","cc":"IND","rank":4043,"id":null},{"ID":1304,"Name":"Bangalore Intl Airport","cc":"IND","rank":8,"id":null},{"ID":94616,"Name":"Bangarupalem","cc":"IND","rank":1,"id":null},{"ID":198581,"Name":"Bangarapet","cc":"IND","rank":0,"id":null},{"ID":94208,"Name":"Bangana","cc":"IND","rank":0,"id":null},{"ID":199245,"Name":"Bangaon","cc":"IND","rank":0,"id":null},{"ID":81774,"Name":"Bangalore Package","cc":"IND","rank":0,"id":null},{"ID":78517,"Name":"Bangalore Sight Seeing","cc":"IND","rank":0,"id":null},{"ID":202000,"Name":"Wonderla Amusement Park(bangalore)","cc":"IND","rank":0,"id":null},{"ID":202037,"Name":"Wonderla (bangalore) Package","cc":"IND","rank":0,"id":null}]}}

我编写的代码:

String soapEndPointURL= "https://www.redbus.in/Home/SolarSearch?search=banga";

          HttpClient client = new HttpClient();

              GetMethod method = new GetMethod(soapEndPointURL);
              int response_code = client.executeMethod(method);
              System.out.println("Response : " + response_code);
              System.out.println("HTTP GET request status: " + method.getStatusText());
              String response_text= method.getResponseBodyAsString();
              System.out.println(response_text);
            try{
                JSONObject jsonObj = new JSONObject(response_text);
              JSONObject myResponse = jsonObj.getJSONObject("response");
              JSONArray tsmresponse = (JSONArray) myResponse.get("docs");

              ArrayList<String> list = new ArrayList<String>();

              for(int i=0; i<tsmresponse.length(); i++){
                 String s = tsmresponse.getJSONObject(i).getString("Name");
                 System.out.println(s);
              }
    }
            catch(JSONException e)
            {
                System.out.println(e);
            }
}

1 个答案:

答案 0 :(得分:0)

I got solution but now again i'm not getting how to identify that particular object is an array or not.. (Isarray() method not working for `jsonarray`)

package Test.API;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RedBusJSONResponse {
    public static void main(String[] args) throws HttpException, IOException {
        String soapEndPointURL = "https://www.redbus.in/Home/SolarSearch?search=banga";

        HttpClient client = new HttpClient();

        GetMethod method = new GetMethod(soapEndPointURL);
        int response_code = client.executeMethod(method);

        String response_text = method.getResponseBodyAsString();
        System.out.println(response_text);
        try {
            JSONObject jsonObj = new JSONObject(response_text);
            System.out.println(jsonObj);
            JSONArray mainKeyArr =  jsonObj.names();
            System.out.println(mainKeyArr);

            for (int i=0;i<mainKeyArr.length();i++) {
                String mainkey = mainKeyArr.get(i).toString();
                JSONObject subKeyArr = jsonObj.getJSONObject(mainkey);
                JSONArray subTwoKey = subKeyArr.names();
                System.out.println(subTwoKey);
                //Need to write code to identify whether object is an array or not
            }
           }
            catch (JSONException e) {
            System.out.println(e);
        }
    }
}


getting output is :

["response","responseHeader"]
["docs","numFound","start"]
["QTime","params","status"]