How to improve this code

Asked

Viewed 82 times

3

A few 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" by 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 exit 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 couldn’t isolate the category fields main since they were all in one String only.

So I created several auxiliary strings to split, separate what I wanted to display and then cut the characters that I didn’t want to be printed.

Could you tell me how to do this in a more practical way?

Thank you

1 answer

4


Since you have a JSON the ideal is to navigate all the information through the JSON parser methods instead of using regexs and/or splits.

Using org.json

The most direct way to obtain temperature, which is in this case the property temp of your JSON, would be:

  • Access the object first main with getJSONObject
  • About this object now access the field temp with getDouble

Applying to your code:

JSONObject condicao = obj.getJSONArray("weather").getJSONObject(0);
System.out.println("O tempo está: " + condicao.getString("description"));
JSONObject mainObj = obj.getJSONObject("main"); //como JSONObject e não String
System.out.println("A temperatura neste momento é de " + mainObj.getDouble("temp") + "º");

Using twitter4j

Although the logic to apply to this library is the same, there is in fact no method getDouble that gets the value directly as Double.

You can use the method instead get that returns a Object and that in the case of the value to be interpreted this is a decimal number object will be a Double, having practically the same effect as the getDouble of org.json.

So only the last line needs to be different:

System.out.println("A temperatura neste momento é de " + mainObj.get("temp") + "º");
  • Exact. Basically the friend did not know that it is possible to access a nested object through the method getJSONObject.

  • Thank you, Isaac. The code passed by you presented problem with the getDouble which, according to Netbeans does not exist in the class, but I resolved with mainObj.get("temp") or mainObj.getString("temp") . I say this so you can edit the answer and help more people with the same question. P.S. my JSON library used is the Twitter4j since I am also implementing integration with the social network API in the application

  • 1

    @You’re welcome. I assumed you were using org.json and so it didn’t match/work with the library you were using. Thank you for warning, that so I have updated the response to contemplate both solutions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.