I have a problem and I can't figure out how to solve it.
I have the following code:
public static void main(String[] args)
{
BufferedReader br = null;
StringBuilder sb = null;
String line = null;
Scanner scanner = new Scanner(System.in);
//print the menu
System.out.println("Choose from the menu:");
System.out.println("1 -> Town weather ");
System.out.println("2 -> About");
System.out.println("3 -> Exit");
try
{
//read from keyboard the value
//int choice = scanner.nextInt();
int choice = 1;
switch (choice)
{
case 1:
System.out.println("Give desired town:");
String town = str.nextLine();
URL json = new URL("http://api.openweathermap.org/data/2.5/weather?q=" + town + "&APPID=");
HttpURLConnection url = (HttpURLConnection) json.openConnection();
url.setRequestMethod("GET");
url.connect();
//read the data from url
br = new BufferedReader(new InputStreamReader(url.getInputStream()));
sb = new StringBuilder();
while ((line = br.readLine()) != null)
{
sb.append(line + '\n');
}
String txt = sb.toString();
break;
case 2:
break;
case 3:
System.exit(0);
}
}
catch (InputMismatchException i)
{
System.out.println("Wrong choice!");
}
catch (MalformedURLException m)
{
System.out.println("Wrong URL!");
}
catch (IOException io)
{
System.out.println("Wrong town! The url shows 404 not found");
}
catch (NullPointerException np)
{
System.out.println("Null exception!");
np.printStackTrace();
}
catch (Exception e) //catch all exception where not previous caught.
{
e.printStackTrace();
}
}
So I have the json data into txt variable. The problem is, that my project requires to show all the data (or a part of them), as a list. I must show them in such a way that a human can read them.
I tried pretty printing, but I do not want to show the symbols { } , :
Ideally, I would like to store these data into a database and then show some of them, while I maintain them. The first step, is to split them, in an array, only the strings and none of the special characters.
Can anyone help me with this? I searched stackoverflow, and I found many responses, but none worked for me.
EDIT: I updated the code, with my full main and below you can see the json response:
{
"coord": {
"lon": -86.97,
"lat": 34.8
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 270.48,
"pressure": 1033,
"humidity": 30,
"temp_min": 270.15,
"temp_max": 271.15
},
"visibility": 16093,
"wind": {
"speed": 2.6,
"deg": 340
},
"clouds": {
"all": 1
},
"dt": 1514921700,
"sys": {
"type": 1,
"id": 226,
"message": 0.0021,
"country": "US",
"sunrise": 1514897741,
"sunset": 1514933354
},
"id": 4830668,
"name": "Athens",
"cod": 200
}
Thank you in advance for your help.