1
    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;

}

I am getting following response

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"    ]  ]}

I need to fill the response data in following variables .

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;

Would appreciate if anyone can help me on it.

Bhupendra Singh Rautela
  • 3,406
  • 6
  • 21
  • 25
  • Possible duplicate of [How to convert the following json string to java object?](https://stackoverflow.com/questions/10308452/how-to-convert-the-following-json-string-to-java-object) – jpeg Sep 13 '19 at 06:20

3 Answers3

0

You can use any JSON to Java unmarshalling library to convert the JSON to Java object. Check options and examples

Adisesha
  • 5,200
  • 1
  • 32
  • 43
0

Create JSONObject of the response string you are getting and then extract values fields from JSONObject as JSONArray and then traverse through that JSONArray to get list of your object.

hiren
  • 1,742
  • 13
  • 20
0

To add to Hiren's answer, you can try (using 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);
     }

Now the "values" part of response will be stored in String[] values

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

This will print Lead Data Set 1 - Normal FOC Lead Bhupendra bhupendra+283273@abc.com 2389432432 90909892098988771 Street123, Berlin 1289243424321 no comments 10115 12 / 12 / 2017

You can assign it accordingly. Hope it helps.

Divya Jha
  • 21
  • 5