0

I am currently trying to get the values from external API that is written in json format. I succeeded on getting data from this api. but I still can't extract the specific value from it..

this is the nasa api that I want to extract some datas from https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=DEMO_KEY

and I tried this code

String reqURL = "https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=my key name";

        URL url = new URL(reqURL);
        
        
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        
   
        conn.setRequestMethod("GET");
        conn.setDoOutput(true);
        
        int res_Code = conn.getResponseCode();
        
        //System.out.println(res_Code);I got 200 .
        

/* I first tried to get value of key "links" , but I didnt get the values... */

        if(res_Code == 200) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuffer result = new StringBuffer();
            
            String line = null;
            
            while((line = br.readLine())!=null) {
                result.append(line);
            }
            
            System.out.println(result);
            //I got json data that is converted to string.

            //converting string to json 
             JSONParser pars = new JSONParser(result.toString());              
             Object obj = pars.parse();
             JSONObject json = (JSONObject)obj;

            //get the value from key 'links'
             String value = (String)json.get("links"); 
             System.out.println(value); // not showing..

             br.close();

I tried to do alot of times but I keep failing.. If my way of algorithm is wrong . please let me know ! I would like to get the value from key called 'links' or 'next'

enter image description here

I would be so greateful if someone let me know .. thank you so much

정지은
  • 25
  • 3

0 Answers0