Some days ago I asked for help here in the OS to parse JSON in Java and I got what I wanted, however, I had to do a "gambiarra" for not knowing a better way.
It's working, but I'm sure the code can be improved.
My JSON is as follows:
"weather": [
{
"id": 800,
"main": "Clear",
"description": "tempo claro",
"icon": "01n" }
],
"main": {
"temp": 16.29,
"pressure": 1020,
"humidity": 77,
"temp_min": 16.29
},
My output is
O tempo está: tempo claro
A temperatura neste momento é de 16.29º
and the code to generate my output was as follows
JSONObject condicao = obj.getJSONArray("weather").getJSONObject(0);
escritor.println("O tempo está: " + condicao.getString("description"));
String n = obj.getString("main");
String t [] = n.split(Pattern.quote (","));
String x = t[0];
escritor.println("A temperatura neste momento é de " + x.substring(8,13) + "º");
As you can see I could not isolate the fields from the main
category since they were all in a String
only.
So I created several auxiliary strings to split, separate what I wanted to display, and then cut the characters I did not want to be printed.
Could you tell me how to do this in a more practical way?
Thank you