-1

I receive this string in json format from my frontend:

{"hosts":[{"name":"localhost"},{"name":"localhost"},{"name":"localhost"}]}

What I want to do is to iterate each one of the hosts and print their name.

How do I map my string to do that using java?

pedro almeida
  • 123
  • 10
  • Use [org.json](https://repo1.maven.org/maven2/org/json/json/20180813/json-20180813.jar) then `import org.JSONObject` in your project then `JSONObject myJSONObject = new JSONObject(yourJSONString);` and `List hosts = myObject.getJSONArray("hosts").toList();` and voilà. – Paul Jul 15 '19 at 14:48

1 Answers1

-1

You can convert String to JsonObject with Gson

Other option: use Jackson lib to convert Json string to Java Object

tony2011
  • 13
  • 2