流并创建JSON对象列表

时间:2015-07-14 12:59:17

标签: java json serialization

我遇到了以下问题。我有一个字符串,我想编码为JSON(丢弃它携带的一些信息)。字符串如下:

{
"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": "nat-policy" }, { "token": "inbound" }, { "token": "X1" }, { "token": "outbound" }, { "token": "X1" }, { "token": "destination" }, { "token": "name" }, { "token": "WAN Primary IP" }, { "token": "service" }, { "token": "group" }, { "token": "Ping" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "1" } ] },
        { "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": "group" }, { "token": "Ping" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "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": "SSH Management" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "2" } ] },
        { "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": "SSH Management" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "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": "HTTPS Management" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "3" } ] },
        { "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": "HTTPS Management" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "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": "HTTP Management" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "4" } ] },
        { "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": "HTTP Management" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "X0" }, { "token": "outbound" }, { "token": "X0" }, { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" }, { "token": "service" }, { "token": "group" }, { "token": "Ping" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "5" } ] },
        { "command": [ { "token": "inbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "outbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "source" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-source" }, { "token": "original" } ] },
        { "command": [ { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" } ] },
        { "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
        { "command": [ { "token": "service" }, { "token": "group" }, { "token": "Ping" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "X0" }, { "token": "outbound" }, { "token": "X0" }, { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" }, { "token": "service" }, { "token": "name" }, { "token": "SSH Management" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "6" } ] },
        { "command": [ { "token": "inbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "outbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "source" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-source" }, { "token": "original" } ] },
        { "command": [ { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" } ] },
        { "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
        { "command": [ { "token": "service" }, { "token": "name" }, { "token": "SSH Management" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "X0" }, { "token": "outbound" }, { "token": "X0" }, { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" }, { "token": "service" }, { "token": "name" }, { "token": "HTTPS Management" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "7" } ] },
        { "command": [ { "token": "inbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "outbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "source" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-source" }, { "token": "original" } ] },
        { "command": [ { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" } ] },
        { "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
        { "command": [ { "token": "service" }, { "token": "name" }, { "token": "HTTPS Management" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "X0" }, { "token": "outbound" }, { "token": "X0" }, { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" }, { "token": "service" }, { "token": "name" }, { "token": "HTTP Management" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "8" } ] },
        { "command": [ { "token": "inbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "outbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "source" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-source" }, { "token": "original" } ] },
        { "command": [ { "token": "destination" }, { "token": "name" }, { "token": "LAN Primary IP" } ] },
        { "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
        { "command": [ { "token": "service" }, { "token": "name" }, { "token": "HTTP Management" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Management NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "any" }, { "token": "outbound" }, { "token": "X1" }, { "token": "source" }, { "token": "group" }, { "token": "All Interface IP" }, { "token": "translated-source" }, { "token": "name" }, { "token": "WAN Primary IP" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "9" } ] },
        { "command": [ { "token": "inbound" }, { "token": "any" } ] },
        { "command": [ { "token": "outbound" }, { "token": "X1" } ] },
        { "command": [ { "token": "source" }, { "token": "group" }, { "token": "All Interface IP" } ] },
        { "command": [ { "token": "translated-source" }, { "token": "name" }, { "token": "WAN Primary IP" } ] },
        { "command": [ { "token": "destination" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
        { "command": [ { "token": "service" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Auto-added X1 Default NAT Policy" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },

    { "command": [ { "token": "nat-policy" }, { "token": "inbound" }, { "token": "X0" }, { "token": "outbound" }, { "token": "X1" }, { "token": "translated-source" }, { "token": "name" }, { "token": "WAN Primary IP" } ] },
    { "submode": [
        { "command": [ { "token": "id" }, { "token": "10" } ] },
        { "command": [ { "token": "inbound" }, { "token": "X0" } ] },
        { "command": [ { "token": "outbound" }, { "token": "X1" } ] },
        { "command": [ { "token": "source" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-source" }, { "token": "name" }, { "token": "WAN Primary IP" } ] },
        { "command": [ { "token": "destination" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-destination" }, { "token": "original" } ] },
        { "command": [ { "token": "service" }, { "token": "any" } ] },
        { "command": [ { "token": "translated-service" }, { "token": "original" } ] },
        { "command": [ { "token": "enable" } ] },
        { "command": [ { "token": "comment" }, { "token": "Auto-added X0 outbound NAT Policy for X1 WAN" } ] },
        { "command": [ { "token": "exit" } ] }
    ] },


    { "command": [ { "token": "end" } ] }
] }

此字符串是Sonicwall路由器的响应,列出了所有已保存的NAT策略。我正在做的是使用这部分代码流(我正在使用JSON处理库),删除不需要的信息并从上面创建一个新的JSON对象。

byte[] jsonExampleByte = jsonExample.getBytes();
    ByteArrayInputStream stream = new ByteArrayInputStream(jsonExampleByte);
    InputStreamReader input = new InputStreamReader(stream);
    JsonObject myJson = null;

    JsonReader reader = Json.createReader(stream);
    JsonObject jObject = reader.readObject();

    System.out.println("JSON Object: " + jObject.toString());

    JsonBuilderFactory factory = Json.createBuilderFactory(null);

    myJson = factory.createObjectBuilder().add("success", jObject.get("success")).add("cli", factory.createArrayBuilder().add(factory.createObjectBuilder().add("submode", jObject.getJsonArray("cli").getJsonObject(1).get("submode")))).build();

    FileOutputStream fs = new FileOutputStream("my_new_json");
    JsonWriter wr = Json.createWriter(fs);
    wr.writeObject(myJson);
    wr.close();

我遇到的问题是即使上面的代码很好用。我只能创建一个新的JSON对象(包含第一个)。我想要做的是从输入流创建一堆新对象并将它们存储在列表中。任何人都可以帮忙吗?

0 个答案:

没有答案