Question: I am currently having an issue with a code which is below. The first half executed fine but when I add the json portion which
I am currently having an issue with a code which is below. The first half executed fine but when I add the json portion which is the second have I have errors. I am not sure what I am doing wrong. Can you please look at it and let me know what is wrong?
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package ceis420_lab1_part_b;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Scanner;
/**
*
* @author dante
*/
public class CEIS420_Lab1_Part_B {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s1="http://api.geonames.org/postalCodeLookupJSON?postalcode=";
String s2="";
System.out.println("Please enter the postal code: ");
s2 = input.nextLine(); //Ask the user for the country code
String s3 = s1+s2+"&country=US&username=devry";
//concatenate string for the RESTful web service
URL url;
try {
url = new URL(s3);//Set up string as a URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content)); //Read data from input stream
JSONParser jpar= new JSONParser();
JSONObject obj = (JSONObject)jpar.parse(new InputStreamReader(content, "UTF-8"));
System.out.println("JSON Object: " + obj);
JSONArray ja = (JSONArray)obj.get("postalcodes");
Iterator i = ja.iterator();
while(i.hasNext())
{
JSONObject j = (JSONObject) i.next();
Double lat = (Double) j.get("lat");
Double lng = (Double) j.get("lng");
System.out.println("Latitude is "+lat.toString()+" and Longitdue is "+lng.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I build this is the error
ant -f C:\\Users\\dante\\OneDrive\\Documents\\NetBeansProjects\\CEIS420_Lab1_Part_B -Dnb.internal.action.name=debug -Ddebug.class=ceis420_lab1_part_b.CEIS420_Lab1_Part_B debug
init:
deps-jar:
Created dir: C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\build
Updating property file: C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\build\built-jar.properties
Created dir: C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\build\classes
Created dir: C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\build\empty
Created dir: C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\build\generated-sources\ap-source-output
Compiling 4 source files to C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\build\classes
C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\src\ceis420_lab1_part_b\CEIS420_Lab1_Part_B.java:47: error: cannot find symbol
JSONObject obj = (JSONObject)jpar.parse(new InputStreamReader(content, "UTF-8"));
^
symbol: method parse(InputStreamReader)
location: variable jpar of type JSONParser
C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\src\ceis420_lab1_part_b\CEIS420_Lab1_Part_B.java:52: error: cannot find symbol
JSONArray ja = (JSONArray)obj.get("postalcodes");
symbol: method get(String)
location: variable obj of type JSONObject
C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\src\ceis420_lab1_part_b\CEIS420_Lab1_Part_B.java:60: error: cannot find symbol
Double lat = (Double) j.get("lat");
symbol: method get(String)
location: variable j of type JSONObject
C:\Users\dante\OneDrive\Documents\NetBeansProjects\CEIS420_Lab1_Part_B\src\ceis420_lab1_part_b\CEIS420_Lab1_Part_B.java:61: error: cannot find symbol
Double lng = (Double) j.get("lng");
symbol: method get(String)
location: variable j of type JSONObject
4 errors
BUILD FAILED (total time: 4 seconds)
I am not sure what I am doing wrong. Any help will be greatly appreciated.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
