java - 如何将googlesheet api响应存储到java中的数组?
问题描述
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;
如果有人可以帮助我,将不胜感激。
解决方案
创建JSONObject
您正在获取的响应字符串,然后从中提取values
字段JSONObject
,JSONArray
然后遍历该 JSONArray 以获取您的对象列表。
推荐阅读
- python - OSError:[Errno 22] 无效参数:'
' - vba - 如果 A1 到列尾没有值,则停止该过程
- arrays - 使用 put in for 循环以表格格式打印名称,但仅正确打印姓氏
- wordpress - WP重置出错
- angular - CORS 访问控制允许来源 iOS
- javascript - Browserify @yimura/scraper - 未捕获(承诺中)类型错误:“侦听器”参数必须是函数类型。接收到的类型对象
- python - 这个 python selenium 程序不迭代
- python - 如何在 y 轴上同时绘制日期和时间
- ruby-on-rails - 从 rack-attack 6.3.1 升级到 6.5.0 导致自定义方法出错
- c# - 如何使int随机数