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?
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.