【问题标题】:How can I store googlesheet api response to an array in java?如何将googlesheet api响应存储到java中的数组?
【发布时间】:2019-09-13 06:07:37
【问题描述】:
    public static String getsheetdata() throws IOException {

    String name = null;
    String email = null;
    String phone = null;
    String fin = null;
    String address = null;
    String car_registraion = null;
    String question = null;
    String pin = null;
    String car_registraion_date = null;

     String url = "https://sheets.googleapis.com/v4/spreadsheets/1BH-e3-XSZ9LjsQqELjZLpZbnB4DmIhrPy2VDAZsP9KM/values/lead!A2:J2?key=AIzaSyDJRy73ru1BSLFCb9nknUF8SlZd4LxwJAc";

     URL obj = new URL(url);
     HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
     // optional default is GET
     con.setRequestMethod("GET");
     //add request header
     con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5");

     int responseCode = con.getResponseCode();
     System.out.println("\nSending 'GET' request to URL : " + url);
     System.out.println("Response Code : " + responseCode);
     BufferedReader in = new BufferedReader(
             new InputStreamReader(con.getInputStream()));
     String inputLine;
     StringBuffer response = new StringBuffer();
     while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
     }
     in.close();

     System.out.println(response.toString());
     //Read JSON response and print
     JSONObject myResponse = new JSONObject(response.toString());


    return inputLine;

}

我收到以下回复

Sending 'GET' request to URL : https://sheets.googleapis.com/v4/spreadsheets/1BH-e3-XSZ9LjsQqELjZLpZbnB4DmIhrPy2VDAZsP9KM/values/lead!A2:J2?key=AIzaSyDJRy73ru1BSLFCb9nknUF8SlZd4LxwJAc
Response Code : 200
{  "range": "lead!A2:J2",  "majorDimension": "ROWS",  "values": [    [      "Lead Data Set 1 -  Normal FOC Lead",      "Bhupendra",      "bhupendra+283273@abc.com",      "2389432432",      "90909892098988771",      "Street123, Berlin",      "1289243424321",      "no comments",      "10115",      "12 / 12 / 2017"    ]  ]}

我需要在以下变量中填写响应数据。

String name = null;
String email = null;
String phone = null;
String fin = null;
String address = null;
String car_registraion = null;
String question = null;
String pin = null;
String car_registraion_date = null;

如果有人可以帮助我,将不胜感激。

【问题讨论】:

标签: java arrays rest google-sheets-api httpsurlconnection


【解决方案1】:

您可以使用任何 JSON 到 Java 解组库来将 JSON 转换为 Java 对象。检查options and examples

【讨论】:

    【解决方案2】:

    创建您正在获取的响应字符串的JSONObject,然后从JSONObject 中提取values 字段为JSONArray,然后遍历该JSONArray 以获取您的对象列表。

    【讨论】:

      【解决方案3】:

      要添加到 Hiren 的答案,您可以尝试(使用 org.json):

           JSONObject myResponse = new JSONObject(response.toString());
           JSONArray jsonArr = (JSONArray) myResponse.get("values");
           JSONArray requiredValues = jsonArr.getJSONArray(0);
           String[] values = new String[requiredValues.length()];
      
           for (int i = 0; i < requiredValues.length(); i++) {
              values[i] = requiredValues.getString(i);
           }
      

      现在响应的"values"部分将存储在String[] values

          for (int j = 0; j < values.length; j++) {
               System.out.println(values[j]);
          }
      

      这将打印 Lead Data Set 1 - Normal FOC Lead Bhupendra bhupendra+283273@abc.com 2389432432 90909892098988771 Street123, Berlin 1289243424321 no comments 10115 12 / 12 / 2017

      您可以相应地分配它。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-22
        • 2019-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        • 1970-01-01
        • 2022-06-29
        相关资源
        最近更新 更多