在代码后面创建treeview json

时间:2014-08-12 07:23:44

标签: c# json treeview

我想在代码后面创建json数据,如下面的树视图组件示例;

我可以像第一个那样解析,但我不知道如何生成其他人 (例如; tree_data [' for-sale'] [' additionalParameters'])

示例数据

    var tree_data = {
        'for-sale': { name: 'For Sale', type: 'folder' },
        'vehicles': { name: 'Vehicles', type: 'folder' },
        'rentals': { name: 'Rentals', type: 'folder' },
        'real-estate': { name: 'Real Estate', type: 'folder' },
        'pets': { name: 'Pets', type: 'folder' },
        'tickets': { name: 'Tickets', type: 'item' },
        'services': { name: 'Services', type: 'item' },
        'personals': { name: 'Personals', type: 'item' }
    }

    tree_data['for-sale']['additionalParameters'] = {
        'children': {
            'appliances': { name: 'Appliances', type: 'item' },
            'arts-crafts': { name: 'Arts & Crafts', type: 'item' },
            'clothing': { name: 'Clothing', type: 'item' },
            'computers': { name: 'Computers', type: 'item' },
            'jewelry': { name: 'Jewelry', type: 'item' },
            'office-business': { name: 'Office & Business', type: 'item' },
            'sports-fitness': { name: 'Sports & Fitness', type: 'item' }
        }
    }

    tree_data['vehicles']['additionalParameters'] = {
        'children': {
            'cars': { name: 'Cars', type: 'folder' },
            'motorcycles': { name: 'Motorcycles', type: 'item' },
            'boats': { name: 'Boats', type: 'item' }
        }
    }

    tree_data['vehicles']['additionalParameters']['children']['cars']['additionalParameters'] = {
        'children': {
            'classics': { name: 'Classics', type: 'item' },
            'convertibles': { name: 'Convertibles', type: 'item' },
            'coupes': { name: 'Coupes', type: 'item' },
            'hatchbacks': { name: 'Hatchbacks', type: 'item' },
            'hybrids': { name: 'Hybrids', type: 'item' },
            'suvs': { name: 'SUVs', type: 'item' },
            'sedans': { name: 'Sedans', type: 'item' },
            'trucks': { name: 'Trucks', type: 'item' }
        }
    }

    tree_data['rentals']['additionalParameters'] = {
        'children': {
            'apartments-rentals': { name: 'Apartments', type: 'item' },
            'office-space-rentals': { name: 'Office Space', type: 'item' },
            'vacation-rentals': { name: 'Vacation Rentals', type: 'item' }
        }
    }

    tree_data['real-estate']['additionalParameters'] = {
        'children': {
            'apartments': { name: 'Apartments', type: 'item' },
            'villas': { name: 'Villas', type: 'item' },
            'plots': { name: 'Plots', type: 'item' }
        }
    }

    tree_data['pets']['additionalParameters'] = {
        'children': {
            'cats': { name: 'Cats', type: 'item' },
            'dogs': { name: 'Dogs', type: 'item' },
            'horses': { name: 'Horses', type: 'item' },
            'reptiles': { name: 'Reptiles', type: 'item' }
        }
    }

1 个答案:

答案 0 :(得分:1)

  

JSON字符串的所有键和值对都必须用'“'(双引号)括起来。   要使用JSON字符串分配变量,您必须执行以下操作:

var tree_data = {
    "for-sale": { "name": "For Sale", "type": "folder" },
    "vehicles": { "name": "Vehicles", "type": "folder" },
    "rentals": { "name": "Rentals", "type": "folder" },
    "real-estate": { "name": "Real Estate", "type": "folder" },
    "pets": { "name": "Pets", "type": "folder" },
    "tickets": { "name": "Tickets", "type": "item" },
    "services": { "name": "Services", "type": "item" },
    "personals": { "name": "Personals", "type": "item" }
}

tree_data["for-sale"]["additionalParameters"] = {
    "children": {
        "appliances": { "name": "Appliances", "type": "item" },
        "arts-crafts": { "name": "Arts & Crafts", "type": "item" },
        "clothing": { "name": "Clothing", "type": "item" },
        "computers": { "name": "Computers", "type": "item" },
        "jewelry": { "name": "Jewelry", "type": "item" },
        "office-business": { "name": "Office & Business", "type": "item" },
        "sports-fitness": { "name": "Sports & Fitness", "type": "item" }
    }
}
  

根据您的要求,所需的JSON如下所示。

[{
    "for-sale": {
        "name": "ForSale",
        "type": "folder",
        "additionalParameters": {
            "children": {
                "appliances": {
                    "name": "Appliances",
                    "type": "item"
                },
                "arts-crafts": {
                    "name": "Arts&Crafts",
                    "type": "item"
                },
                "clothing": {
                    "name": "Clothing",
                    "type": "item"
                },
                "computers": {
                    "name": "Computers",
                    "type": "item"
                },
                "jewelry": {
                    "name": "Jewelry",
                    "type": "item"
                },
                "office-business": {
                    "name": "Office&Business",
                    "type": "item"
                },
                "sports-fitness": {
                    "name": "Sports&Fitness",
                    "type": "item"
                }
            }
        }
    },
    "vehicles": {
        "name": "Vehicles",
        "type": "folder"
    },
    "rentals": {
        "name": "Rentals",
        "type": "folder"
    },
    "real-estate": {
        "name": "RealEstate",
        "type": "folder"
    },
    "pets": {
        "name": "Pets",
        "type": "folder"
    },
    "tickets": {
        "name": "Tickets",
        "type": "item"
    },
    "services": {
        "name": "Services",
        "type": "item"
    },
    "personals": {
        "name": "Personals",
        "type": "item"
    }
}]