How to extract data from a JSON from Java

0

Hi, I'm trying to use the google API Snapp to road, the problem is that it returns me a JSON and I do not know how to extract latitude and longitude since I want to store them in a database, I'm using java, I would like to the help this is the Json that gives me back the Google API.

    {
  "snappedPoints": [
    {
      "location": {
        "latitude": -2.2302176,
        "longitude": -79.8991927
      },
      "originalIndex": 1,
      "placeId": "ChIJS92r-8pvLZARddN38ctiCxA"
    },
    {
      "location": {
        "latitude": -2.2301738999999996,
        "longitude": -79.8991925
      },
      "placeId": "ChIJS92r-8pvLZARddN38ctiCxA"
    },
    {
      "location": {
        "latitude": -2.2301705063679917,
        "longitude": -79.899192182323432
      },
      "originalIndex": 2,
      "placeId": "ChIJS92r-8pvLZARddN38ctiCxA"
    },
    {
      "location": {
        "latitude": -2.2297732999999997,
        "longitude": -79.899155
      },
      "placeId": "ChIJS92r-8pvLZARddN38ctiCxA"
    },
    {
      "location": {
        "latitude": -2.2297732999999997,
        "longitude": -79.899155
      },
      "placeId": "ChIJUx7M5cpvLZARHT6472dMV1U"
    },
    {
      "location": {
        "latitude": -2.2293598000000006,
        "longitude": -79.899110499999992
      },
      "placeId": "ChIJUx7M5cpvLZARHT6472dMV1U"
    },
    {
      "location": {
        "latitude": -2.2293598000000006,
        "longitude": -79.899110499999992
      },
      "placeId": "ChIJddy658pvLZARx4Epub9ihdE"
    },
    {
      "location": {
        "latitude": -2.2292730000000005,
        "longitude": -79.899101899999991
      },
      "placeId": "ChIJddy658pvLZARx4Epub9ihdE"
    },
    {
      "location": {
        "latitude": -2.2292730000000005,
        "longitude": -79.899101899999991
      },
      "placeId": "ChIJY5o-28pvLZARlbRNsJQEkPg"
    },
    {
      "location": {
        "latitude": -2.2283567,
        "longitude": -79.8990025
      },
      "placeId": "ChIJY5o-28pvLZARlbRNsJQEkPg"
    },
    {
      "location": {
        "latitude": -2.2283567,
        "longitude": -79.8990025
      },
      "placeId": "ChIJOyCe0spvLZAR75IbRyH9Sag"
    },
    {
      "location": {
        "latitude": -2.2279016000000005,
        "longitude": -79.8989567
      },
      "placeId": "ChIJOyCe0spvLZAR75IbRyH9Sag"
    },
    {
      "location": {
        "latitude": -2.2279016000000005,
        "longitude": -79.8989567
      },
      "placeId": "ChIJwRGfKzVuLZAR1lb1uZJm1WQ"
    },
    {
      "location": {
        "latitude": -2.2279011634546126,
        "longitude": -79.898959772458113
      },
      "originalIndex": 3,
      "placeId": "ChIJwRGfKzVuLZAR1lb1uZJm1WQ"
    },
    {
      "location": {
        "latitude": -2.2277730000000004,
        "longitude": -79.8998618
      },
      "placeId": "ChIJwRGfKzVuLZAR1lb1uZJm1WQ"
    },
    {
      "location": {
        "latitude": -2.2277730000000004,
        "longitude": -79.8998618
      },
      "placeId": "ChIJncEO1DRuLZAR5Gm6rg_2NUk"
    },
    {
      "location": {
        "latitude": -2.2277350496908661,
        "longitude": -79.900129263870781
      },
      "originalIndex": 4,
      "placeId": "ChIJncEO1DRuLZAR5Gm6rg_2NUk"
    },
    {
      "location": {
        "latitude": -2.2277331999999994,
        "longitude": -79.9001423
      },
      "placeId": "ChIJncEO1DRuLZAR5Gm6rg_2NUk"
    },
    {
      "location": {
        "latitude": -2.2277331999999994,
        "longitude": -79.9001423
      },
      "placeId": "ChIJ78Q05zRuLZAR-uJ3m1J04Pk"
    },
    {
      "location": {
        "latitude": -2.2257097999999997,
        "longitude": -79.8998518
      },
      "placeId": "ChIJ78Q05zRuLZAR-uJ3m1J04Pk"
    },
    {
      "location": {
        "latitude": -2.2257097999999997,
        "longitude": -79.8998518
      },
      "placeId": "ChIJ_UMy_zRuLZAR7suWIdhHPpM"
    },
    {
      "location": {
        "latitude": -2.2256774,
        "longitude": -79.9001198
      },
      "placeId": "ChIJ_UMy_zRuLZAR7suWIdhHPpM"
    },
    {
      "location": {
        "latitude": -2.2256685,
        "longitude": -79.9001758
      },
      "placeId": "ChIJ_UMy_zRuLZAR7suWIdhHPpM"
    },
    {
      "location": {
        "latitude": -2.2256685,
        "longitude": -79.9001758
      },
      "placeId": "ChIJB57h9TRuLZAR9_wS1_cbEFY"
    },
    {
      "location": {
        "latitude": -2.2256028,
        "longitude": -79.9005743
      },
      "originalIndex": 5,
      "placeId": "ChIJB57h9TRuLZAR9_wS1_cbEFY"
    }
  ]
}

For this I have been testing Gson but not without success.

Pd: I already have the JSON content returned by the API in a String in the application

method that extracts the JSON

    public void extraerUrl(String url)  {

            StringBuffer texto = new StringBuffer();
            try{
                URL pagina = new URL(url);
                try {
                     HttpURLConnection con = (HttpURLConnection) pagina.openConnection();
                     InputStreamReader in = new InputStreamReader((InputStream) con.getContent());
                     BufferedReader buff = new BufferedReader(in);
                     String linea= "" ;
                    while(linea != null){
                         linea = buff.readLine();
                         if(linea != null){
                            texto.append(linea + "\n"); 
                         }                        
                     }
                     txtFuente.setText(texto.toString());
                } catch (IOException e) {
                    txtFuente.setText("IOException");
                }
            }catch(MalformedURLException e){
                txtFuente.setText("MalformedException");
            }    

            //System.out.println(texto.toString());
           SnappedPoint snap = new SnappedPoint();
           Gson gson = new GsonBuilder().setPrettyPrinting().create();
           snap = gson.fromJson(texto.toString(), SnappedPoint.class);
           System.out.println(snap);
}
    
asked by Luis Garcia 31.12.2017 в 00:00
source

2 answers

1

Try the following code:

    try{
        URL url = new URL("https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836&interpolate=true&key=YOUR_API_KEY");
        try (InputStream is = url.openStream();
             JsonReader rdr = Json.createReader(is)) {
            JsonObject obj = rdr.readObject();
            JsonArray results = obj.getJsonArray("snappedPoints");
            for (JsonObject result : results.getValuesAs(JsonObject.class)) {
                System.out.println(result.getJsonObject("location").get("latitude"));
            }
        }
    }catch ( Exception e){
        e.printStackTrace();
    }

You need the following dependencies:

    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.1</version>
    </dependency>

Let me know if it works for you.

    
answered by 31.12.2017 в 03:25
0

Well apparently the answer was simple

Resume that the JSON has an array and nested to the other JSON object which is called location I only had to obtain the dataset twice since with the first one everything is obtained and with the second one the location that is what interests me . My programming is not improved but I leave as I can.

ArrayList<SnappedPoint> snapptedpoints = new ArrayList<SnappedPoint>();
JsonParser parser = new JsonParser();
     JsonElement element = parser.parse(texto.toString());
                    if (element.isJsonObject()) {
                    JsonObject snap = element.getAsJsonObject();    
                    JsonArray datasets = snap.getAsJsonArray("snappedPoints");
                    for (int i = 0; i <datasets.size(); i++) {
                        JsonObject dataset = datasets.get(i).getAsJsonObject();
                        JsonObject dataset2 = dataset.getAsJsonObject("location");
                        JsonElement latitude = dataset2.get("latitude");
                        JsonElement longitude = dataset2.get("longitude");
                        double latitud = latitude.getAsDouble();
                        double longitud = longitude.getAsDouble();
                        LatLng latlon = new LatLng(latitud, longitud);
                        SnappedPoint punto = new SnappedPoint(latlon);
                        snapptedpoints.add(punto);     
                }

            }
         for(int i=0; i<snapptedpoints.size();i++){
          System.out.println(snapptedpoints.get(i).toString());
         }
    
answered by 31.12.2017 в 04:32