import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class ParseWorkFlow { public static void main (String[] args) throws java.lang.Exception { String USER_AGENT = "Mozilla/5.0"; final String request = "https://api.vk.com/method/database.getUniversities"; int[] CitiesToParse = {1, 2}; //массив с нужными городами List<String> universities = new ArrayList<>(); //массив с результатом парса(все еще в JSON) for(int i = 0; i < CitiesToParse.length; i++) { String urlString = request + "?city_id=" + (int)CitiesToParse[i] + "&count=2"; URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String output; StringBuffer response = new StringBuffer(); while ((output = in.readLine()) != null) { response.append(output); } in.close(); String strParseResult = response.toString(); System.out.println(strParseResult); universities.add(strParseResult); } } } 

At the end of the code for the test led the result to a string and displayed on the screen.

The code issues the following:

 {"response":[713,{"id":2,"title":"МГУ"},{"id":86,"title":"АПИ при ИГиП РАН"}]} {"response":[289,{"id":1,"title":"СПбГУ"},{"id":5,"title":"АРБ им. Вагановой"}]} 

There is a GSON library. With her I want to parse the result.

I insert in http://www.jsonschema2pojo.org/ , I receive one class. (it seems to me not exactly what you need)

Help parse. The whole brain broke already.

  • String urlString = request +? Country_id = 1 "+" & city_id = "+ (int) CitiesToParse [i] +" & count = "+ (int) count +" & fields = bdate "+" & v = 5.60 "; After added the last two parameters to the request to the API, the correct JSON format began to come in. - Katraps

1 answer 1

Everything, the question is resolved. I thought of it.

After the correct query to vk api, we get the correct JSON, parse it with GSON and that's it.

Next, I will refer to the elements of each sparse university by id in the cycle for a list of faculties and departments.