复杂的JSON对象数组到分层树视图

时间:2018-02-02 14:32:34

标签: c# jquery json treeview bootstrap-treeview

我发现很难解决这个问题。我从C#组件获得了一个复杂的JSON,如下所示:

{
  "definitions": {
    "_ExtensionType": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Any": {
          "type": [
            "array",
            "null"
          ],
          "items": {}
        }
      },
      "required": [
        "Any"
      ]
    },
    "CatalogueReference": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "keyCatalogueReference": {
          "type": [
            "string",
            "null"
          ]
        },
        "catalogueReferenceExtension": {
          "$ref": "#/definitions/_ExtensionType"
        }
      },
      "required": [
        "keyCatalogueReference",
        "catalogueReferenceExtension"
      ]
    },
    "Exchange": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "changedFlag": {
          "type": "integer",
          "enum": [
            0,
            1
          ]
        },
        "changedFlagSpecified": {
          "type": "boolean"
        },
        "clientIdentification": {
          "type": [
            "string",
            "null"
          ]
        },
        "deliveryBreak": {
          "type": "boolean"
        },
        "deliveryBreakSpecified": {
          "type": "boolean"
        },
        "denyReason": {
          "type": "integer",
          "enum": [
            0,
            1,
            2,
            3,
            4
          ]
        },
        "denyReasonSpecified": {
          "type": "boolean"
        },
        "historicalStartDate": {
          "type": "string",
          "format": "date-time"
        },
        "historicalStartDateSpecified": {
          "type": "boolean"
        },
        "historicalStopDate": {
          "type": "string",
          "format": "date-time"
        },
        "historicalStopDateSpecified": {
          "type": "boolean"
        },
        "keepAlive": {
          "type": "boolean"
        },
        "keepAliveSpecified": {
          "type": "boolean"
        },
        "requestType": {
          "type": "integer",
          "enum": [
            0,
            1,
            2,
            3,
            4
          ]
        },
        "requestTypeSpecified": {
          "type": "boolean"
        },
        "response": {
          "type": "integer",
          "enum": [
            0,
            1,
            2,
            3,
            4
          ]
        },
        "responseSpecified": {
          "type": "boolean"
        },
        "subscriptionReference": {
          "type": [
            "string",
            "null"
          ]
        },
        "supplierIdentification": {
          "$ref": "#/definitions/InternationalIdentifier"
        },
        "target": {
          "$ref": "#/definitions/Target"
        },
        "subscription": {
          "$ref": "#/definitions/Subscription"
        },
        "filterReference": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "$ref": "#/definitions/FilterReference"
          }
        },
        "catalogueReference": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "$ref": "#/definitions/CatalogueReference"
          }
        },
        "exchangeExtension": {
          "$ref": "#/definitions/_ExtensionType"
        }
      },
      "required": [
        "changedFlagSpecified",
        "clientIdentification",
        "deliveryBreakSpecified",
        "denyReasonSpecified",
        "historicalStartDateSpecified",
        "historicalStopDateSpecified",
        "keepAliveSpecified",
        "requestTypeSpecified",
        "responseSpecified",
        "subscriptionReference",
        "supplierIdentification",
        "target",
        "subscription",
        "filterReference",
        "catalogueReference",
        "exchangeExtension"
      ]
    },
    "FilterReference": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "deleteFilter": {
          "type": "boolean"
        },
        "deleteFilterSpecified": {
          "type": "boolean"
        },
        "filterOperationApproved": {
          "type": "boolean"
        },
        "filterOperationApprovedSpecified": {
          "type": "boolean"
        },
        "keyFilterReference": {
          "type": [
            "string",
            "null"
          ]
        },
        "filterReferenceExtension": {
          "$ref": "#/definitions/_ExtensionType"
        }
      },
      "required": [
        "deleteFilterSpecified",
        "filterOperationApprovedSpecified",
        "keyFilterReference",
        "filterReferenceExtension"
      ]
    },
    "InternationalIdentifier": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "country": {
          "type": "integer",
          "enum": [
            0,
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
            21,
            22,
            23,
            24,
            25,
            26,
            27,
            28,
            29,
            30,
            31,
            32,
            33,
            34,
            35,
            36,
            37,
            38,
            39,
            40,
            41,
            42,
            43,
            44
          ]
        },
        "nationalIdentifier": {
          "type": [
            "string",
            "null"
          ]
        },
        "internationalIdentifierExtension": {
          "$ref": "#/definitions/_ExtensionType"
        }
      },
      "required": [
        "country",
        "nationalIdentifier",
        "internationalIdentifierExtension"
      ]
    },
    "MultilingualString": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "values": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "$ref": "#/definitions/MultilingualStringValue"
          }
        }
      },
      "required": [
        "values"
      ]
    },
    "MultilingualStringValue": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "lang": {
          "type": [
            "string",
            "null"
          ]
        },
        "Value": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "lang",
        "Value"
      ]
    },
    "PayloadPublication": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "feedDescription": {
          "$ref": "#/definitions/MultilingualString"
        },
        "feedType": {
          "type": [
            "string",
            "null"
          ]
        },
        "publicationTime": {
          "type": "string",
          "format": "date-time"
        },
        "publicationCreator": {
          "$ref": "#/definitions/InternationalIdentifier"
        },
        "payloadPublicationExtension": {
          "$ref": "#/definitions/_ExtensionType"
        },
        "lang": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "feedDescription",
        "feedType",
        "publicationTime",
        "publicationCreator",
        "payloadPublicationExtension",
        "lang"
      ]
    },
    "Subscription": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "deleteSubscription": {
          "type": "boolean"
        },
        "deleteSubscriptionSpecified": {
          "type": "boolean"
        },
        "deliveryInterval": {
          "type": "number"
        },
        "deliveryIntervalSpecified": {
          "type": "boolean"
        },
        "operatingMode": {
          "type": "integer",
          "enum": [
            0,
            1,
            2,
            3
          ]
        },
        "subscriptionStartTime": {
          "type": "string",
          "format": "date-time"
        },
        "subscriptionState": {
          "type": "integer",
          "enum": [
            0,
            1
          ]
        },
        "subscriptionStopTime": {
          "type": "string",
          "format": "date-time"
        },
        "subscriptionStopTimeSpecified": {
          "type": "boolean"
        },
        "updateMethod": {
          "type": "integer",
          "enum": [
            0,
            1,
            2
          ]
        },
        "target": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "$ref": "#/definitions/Target"
          }
        },
        "filterReference": {
          "$ref": "#/definitions/FilterReference"
        },
        "catalogueReference": {
          "$ref": "#/definitions/CatalogueReference"
        },
        "subscriptionExtension": {
          "$ref": "#/definitions/_ExtensionType"
        }
      },
      "required": [
        "deleteSubscriptionSpecified",
        "deliveryIntervalSpecified",
        "operatingMode",
        "subscriptionStartTime",
        "subscriptionState",
        "subscriptionStopTimeSpecified",
        "updateMethod",
        "target",
        "filterReference",
        "catalogueReference",
        "subscriptionExtension"
      ]
    },
    "Target": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "address": {
          "type": [
            "string",
            "null"
          ]
        },
        "protocol": {
          "type": [
            "string",
            "null"
          ]
        },
        "targetExtension": {
          "$ref": "#/definitions/_ExtensionType"
        }
      },
      "required": [
        "address",
        "protocol",
        "targetExtension"
      ]
    }
  },
  "type": "object",
  "properties": {
    "exchange": {
      "$ref": "#/definitions/Exchange"
    },
    "payloadPublication": {
      "$ref": "#/definitions/PayloadPublication"
    },
    "d2LogicalModelExtension": {
      "$ref": "#/definitions/_ExtensionType"
    },
    "modelBaseVersion": {
      "type": [
        "string",
        "null"
      ]
    },
    "extensionName": {
      "type": [
        "string",
        "null"
      ],
      "default": "ParkingExtension"
    },
    "extensionVersion": {
      "type": [
        "string",
        "null"
      ],
      "default": "0.3"
    }
  },
  "required": [
    "exchange",
    "payloadPublication",
    "d2LogicalModelExtension",
    "modelBaseVersion",
    "extensionName",
    "extensionVersion"
  ]
}

我希望将这个复杂的JSON转换为父/子树视图,因此我可以在需要JSON的Bootstrap树视图组件中显示它:text(object)/ nodes(array),如下所示:

var tree = [
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        nodes: [
          {
            text: "Grandchild 1"
          },
          {
            text: "Grandchild 2"
          }
        ]
      },
      {
        text: "Child 2"
      }
    ]
  },
  {
    text: "Parent 2"
  },
  {
    text: "Parent 3"
  },
  {
    text: "Parent 4"
  },
  {
    text: "Parent 5"
  }
];

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:0)

您从哪里获取此数据?