我目前正在使用SonicWall路由器。我正在尝试从项目中获取一些信息。我通过SSH连接到它,我选择了JSON作为输出格式来获取所有信息。我遇到的问题如下。对于简单命令,返回的内容非常简单,但对于更复杂的命令,返回的JSON可能无效。
以下是一个简短示例(这些是NAT政策):
{
"success": true,
"cli": [
{ "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "X1" }, { "token": "outbound" }, { "token": "X1" }, { "token": "destination" }, { "token": "name" }, { "token": "WAN Primary IP" }, { "token": "service" }, { "token": "name" }, { "token": "SNMP" } ] },
{ "submode": [
{ "command": [ { "token": "id" }, { "token": "0" } ] },
{ "command": [ { "token": "inbound" }, { "token": "X1" } ] },
{ "command": [ { "token": "outbound" }, { "token": "X1" } ] },
{ "command": [ { "token": "source" }, { "token": "any" } ] },
{ "command": [ { "token": "translated-source" }, { "token": "original" } ] },
{ "command": [ { "token": "destination" }, { "token": "name" }, { "token": "WAN Primary IP" } ] },
{ "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
{ "command": [ { "token": "service" }, { "token": "name" }, { "token": "SNMP" } ] },
{ "command": [ { "token": "translated-service" }, { "token": "original" } ] },
{ "command": [ { "token": "enable" } ] },
{ "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
{ "command": [ { "token": "exit" } ] }
] },
{ "command": [ { "token": "end" } ] }
] }
据我所知,返回JSON包含一个字段"成功"和一个领域" cli"其中包含一个字段"命令"这是一个令牌列表和一个字段" submode"这是一个命令列表,每个命令都包含一个令牌列表。
我想删除字段"命令"以及它所持有的令牌列表,结果将是这样的:
{
"success": true,
"cli": [
{ "submode": [
{ "command": [ { "token": "id" }, { "token": "0" } ] },
{ "command": [ { "token": "inbound" }, { "token": "X1" } ] },
{ "command": [ { "token": "outbound" }, { "token": "X1" } ] },
{ "command": [ { "token": "source" }, { "token": "any" } ] },
{ "command": [ { "token": "translated-source" }, { "token": "original" } ] },
{ "command": [ { "token": "destination" }, { "token": "name" }, { "token": "WAN Primary IP" } ] },
{ "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
{ "command": [ { "token": "service" }, { "token": "name" }, { "token": "SNMP" } ] },
{ "command": [ { "token": "translated-service" }, { "token": "original" } ] },
{ "command": [ { "token": "enable" } ] },
{ "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
{ "command": [ { "token": "exit" } ] }
] }
] }
本质上我只想保留"子模式"带命令列表的字段。
我在Java中这样做,并且找不到一种方法来对返回的JSON进行子串,以获得预期的结果。任何人都可以帮忙吗?
答案 0 :(得分:0)
您可以使用java jsonp:
JsonReader reader = Json.createReader(myJsonStream);
JsonObject obj = reader.readObject();
JsonBuilderFactory factory = Json.createBuilderFactory(null);
JsonObject myJson = factory.createObjectBuilder()
.add("success", obj.get("success"))
.add("cli", factory.createArrayBuilder()
.add(factory.createObjectBuilder()
.add("submode", obj.getJsonArray("cli").getJsonObject(1).get("submode")))).build();
FileOutputStream fs = new FileOutputStream("my_new_json_path");
JsonWriter wr = Json.createWriter(fs);
wr.writeObject(myJson);
wr.close();
绑定到你的json结构但是它有效!