Reputation: 19632
Below is my JSON String. I am trying to extract hosts
from it.
{"description":"DescA","process_running":"sf","hosts":{"dc1":["machineA","machineB"]}}
Since hosts is a JSON String in itself and I would like to extract that JSON String from it. I am using GSON here -
String ss = new String(bytes, Charset.forName("UTF-8"));
// here ss is the above JSON String
Map<String, Object> data = gson.fromJson(ss, Map.class); // parse
I was trying to use Map
to deserialize but the result it coming like this in the data-
{description=DescA, process_running=sf, hosts={dc1=[machineA, machineB]}}
Is there any way, I can extract hosts
in the JSON format?
Upvotes: 0
Views: 1382
Reputation: 10955
The easiest way I can think of to do this would be to use gson to create a tree of json elements from your map, and ask it to give you just the hosts node:
Map<String, Object> data = gson.fromJson(ss, Map.class); // parse
JsonObject jsonTree = (JsonObject) gson.toJsonTree(data);
String hostsJson = jsonTree.get("hosts").toString();
Upvotes: 1