getJsonObject bring specific field

1

I have an object that comes when selecting a particular button.

The object is coming correct.

System.out.println(filter.getJsonObject("cadastro"));

Result:

{"id":1,"createdAt":"2017-12-22T14:00:55.86","nome":"teste"}

However, I need to get only the name, not the whole object, how can I do it?

I tried System.out.println(filter.getJsonObject("cadastro").getString("nome")); but it did not work.

Thanks in advance.

    
asked by anonymous 11.01.2018 / 15:25

1 answer

1

Two ways to use the name would be:

1 -

 filter.getJsonObject("cadastro").optString("nome", "");

2 -

JSONObject json = filter.getJsonObject("cadastro");
String nome = json.getString("nome");

In the first option it will return an empty string if it can not return the value and in the second option, if it does not return value it will give you a JSONException

    
11.01.2018 / 15:38