Skip to main content
Relanzada por el usuario Comunidad
Relanzada por el usuario Comunidad
se añadieron 1522 caracteres en el cuerpo
Origen Enlace

metodo que extrae el 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);
}

metodo que extrae el 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);
}
Origen Enlace

Como extraer datos de un JSON desde Java

Hola estoy tratando de usar la API de google Snapp to road, el problema es que esta me devuelve un JSON y no se como extraer la latitud y longitud ya que las quiero almacenar en una base de datos, estoy usando java , agraceria la ayuda este es el Json que me devuelve la API de Google.

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

Para esto he estado probando Gson pero no sin éxito.

Pd: ya tengo el contenido JSON devuelto por la API en un String en la aplicación