使用C#基于条件获取json childs

时间:2013-08-23 10:43:50

标签: c# json

[
    {
        "Text": "Topaz Building",
        "Value": "101",
        "Expanded": false,
        "Items": [
            {
                "Text": "Floor1",
                "Value": "102",
                "Expanded": false,
                "Items": [
                    {
                        "Text": "Room1",
                        "Value": "105",
                        "Expanded": false,
                        "Items": [
                            {
                                "Text": "Cab1",
                                "Value": "107",
                                "Expanded": false
                            },
                            {
                                "Text": "Cab2",
                                "Value": "108",
                                "Expanded": false,
                                "Items": [
                                    {
                                        "Text": "Sub1",
                                        "Value": "109",
                                        "Expanded": false
                                    },
                                    {
                                        "Text": "Sub2",
                                        "Value": "110",
                                        "Expanded": false
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "Text": "Room2",
                        "Value": "106",
                        "Expanded": false
                    }
                ]
            },
            {
                "Text": "Floor2",
                "Value": "103",
                "Expanded": false,
                "Items": [
                    {
                        "Text": "Room1",
                        "Value": "111",
                        "Expanded": false
                    },
                    {
                        "Text": "Room2",
                        "Value": "112",
                        "Expanded": false,
                        "Items": [
                            {
                                "Text": "Cab1",
                                "Value": "113",
                                "Expanded": false,
                                "Items": [
                                    {
                                        "Text": "Sub1",
                                        "Value": "115",
                                        "Expanded": false
                                    },
                                    {
                                        "Text": "Sub2",
                                        "Value": "116",
                                        "Expanded": false
                                    }
                                ]
                            },
                            {
                                "Text": "Cab2",
                                "Value": "114",
                                "Expanded": false
                            }
                        ]
                    }
                ]
            },
            {
                "Text": "Floor3",
                "Value": "104",
                "Expanded": false
            }
        ]
    }
]

上面是json,我必须操纵

如果我从他们发送/搜索一个值(105)我需要得到它的所有孩子json数据(107,108,109,110)。请帮助我

1 个答案:

答案 0 :(得分:1)

在C#中设计一些类,通过序列化将JSON数据作为字符串读入,然后使用LINQ进行选择,一直查询Items属性。

using System;
using System.Runtime.Serialization;

namespace YourNamespace
{
    [Serializable]
    [DataContract]
    public class Data
    {
        [DataMember(Name = "Text"]
        public string Text{get;set;}

        [DataMember(Name = "Value"]
        public string Value{get;set;}

        [DataMember(Name = "Expanded"]
        public bool Expanded{get;set;}

        [DataMember(Name = "Items"]
        public Data[] Items{get;set;}
    }
}