getJsonObject bring specific field

Asked

Viewed 68 times

1

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

The object is coming right.

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

Upshot:

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

However, I need to return only the name, not the whole object, as I can do?

I tried to System.out.println(filter.getJsonObject("cadastro").getString("nome")); but it didn’t work out.

From now on, thank you.

  • You are using javax.json?

  • 1

    What happened when you tried the getString("nome")? Build error? Exception? null? A different string came?

1 answer

0

Two ways to recur 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 fails to return the value and in the second option, if it does not return value it will give you a Jsonexception

Browser other questions tagged

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