none
生成树 RRS feed

  • 问题

  • [{ id: "0001", pId: "0000", name: "生活照料",open:false},

    { id: "0002", pId: "0000", name: "家政服务",open:false},

    { id: "0003", pId: "0000", name: "修配服务",open:false},

    { id: "0004", pId: "0000", name: "医疗服务",open:false},

    { id: "0005", pId: "0000", name: "资讯服务",open:false},

    { id: "0006", pId: "0000", name: "文体娱乐",open:false},

    { id: "0007", pId: "0001", name: "代缴费"},

    { id: "0008", pId: "0001", name: "代购物"},

    { id: "0009", pId: "0001", name: "助浴"},

    { id: "0010", pId: "0001", name: "理发"},

    { id: "0011", pId: "0001", name: "修脚"},

    { id: "0012", pId: "0002", name: "打扫卫生"},

    { id: "0013", pId: "0002", name: "清洗厨房用具"},

    { id: "0014", pId: "0002", name: "擦玻璃,窗框,窗纱等"},

    { id: "0015", pId: "0002", name: "清理床上用品及衣服等"},

    { id: "0016", pId: "0002", name: "钟点工"},

    { id: "0017", pId: "0002", name: "上门做饭"},

    { id: "0018", pId: "0003", name: "加点维修"},

    { id: "0019", pId: "0003", name: "上下水管道维修"},

    { id: "0020", pId: "0003", name: "门窗维修"},

    { id: "0021", pId: "0003", name: "开锁,换镜"},

    { id: "0022", pId: "0003", name: "水池,浴缸,坐便器维修"},

    { id: "0023", pId: "0004", name: "提醒和监督按时吃药"},

    { id: "0024", pId: "0004", name: "陪同就医取药"},

    { id: "0025", pId: "0004", name: "简单体检"},

    { id: "0026", pId: "0004", name: "推拿按摩等"},

    { id: "0027", pId: "0005", name: "法律咨询"},

    { id: "0028", pId: "0005", name: "健康咨询"},

    { id: "0029", pId: "0005", name: "心里咨询"},

    { id: "0030", pId: "0005", name: "精神慰藉"},

    { id: "0031", pId: "0006", name: "棋牌"},

    { id: "0032", pId: "0006", name: "歌舞"},

    { id: "0033", pId: "0006", name: "老年网吧"},

    { id: "0034", pId: "0006", name: "体育活动"}

    把上面的代码生成树,代码怎么写


    please verify my account



    • 已编辑 lctk 2016年4月20日 5:41
    • 已编辑 Zhi LvModerator 2016年4月20日 9:06 帮助修改帖子显示格式
    2016年4月20日 5:39

答案

  • Hi lctk,
    你可以参考下下面的链接:
    json转树状结构(js)
    注:如果是想要生成目录导航树,那个需要搭配框架。
    Regards,
    Moonlight

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年4月22日 6:22

全部回复

  • Hi lctk,

    请问你具体要什么效果? 解析当前的Json字符串读入树形图控件还是其他?请你提供一些详细的需求。(你提供的Json字符串我在测试放入解析Json的工具时解析出错)

    有关json与对象互相转换,你可以参考下面的代码:

        //需要引用System.Runtime.Serialization
    
        using System.Runtime.Serialization.Json;
    
        public class JsonHelper
    
        {
    
            #region 对象类型序列化为json 字符
    
            /// <summary>
    
            /// 对象类型序列化为json 字符
    
            /// </summary>
    
            /// <typeparam name="T">实体类型</typeparam>
    
            /// <param name="jsonObject">待转换实体</param>
    
            /// <param name="encoding">编码格式</param>
    
            /// <returns>string</returns>
    
            public static string ObjectToJson<T>(Object jsonObject, Encoding encoding)
    
            {
    
                string result = String.Empty;
    
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    
                {
    
                    serializer.WriteObject(ms, jsonObject);
    
                    result = encoding.GetString(ms.ToArray());
    
                }
    
                return result;
    
            }
    
            #endregion
    
            #region json字符反序列化为对象
    
            /// <summary>
    
            /// json字符反序列化为对象
    
            /// </summary>
    
            /// <typeparam name="T">实体类型</typeparam>
    
            /// <param name="json">json字符串</param>
    
            /// <param name="encoding">编码格式</param>
    
            /// <returns>T</returns>
    
            public static T JsonToObject<T>(string json, Encoding encoding)
    
            {
    
                T resultObject = default(T);
    
                try
    
                {
    
                    resultObject = Activator.CreateInstance<T>();
    
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(encoding.GetBytes(json)))
    
                    {
    
                        resultObject = (T)serializer.ReadObject(ms);
    
                    }
    
                }
    
                catch { }
    
                return resultObject;
    
            }
    
            #endregion
    
        }
    

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年4月20日 9:10
  • Hi lctk,

    请问你具体要什么效果? 解析当前的Json字符串读入树形图控件还是其他?请你提供一些详细的需求。(你提供的Json字符串我在测试放入解析Json的工具时解析出错)

    有关json与对象互相转换,你可以参考下面的代码:

        //需要引用System.Runtime.Serialization
    
        using System.Runtime.Serialization.Json;
    
        public class JsonHelper
    
        {
    
            #region 对象类型序列化为json 字符
    
            /// <summary>
    
            /// 对象类型序列化为json 字符
    
            /// </summary>
    
            /// <typeparam name="T">实体类型</typeparam>
    
            /// <param name="jsonObject">待转换实体</param>
    
            /// <param name="encoding">编码格式</param>
    
            /// <returns>string</returns>
    
            public static string ObjectToJson<T>(Object jsonObject, Encoding encoding)
    
            {
    
                string result = String.Empty;
    
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    
                {
    
                    serializer.WriteObject(ms, jsonObject);
    
                    result = encoding.GetString(ms.ToArray());
    
                }
    
                return result;
    
            }
    
            #endregion
    
            #region json字符反序列化为对象
    
            /// <summary>
    
            /// json字符反序列化为对象
    
            /// </summary>
    
            /// <typeparam name="T">实体类型</typeparam>
    
            /// <param name="json">json字符串</param>
    
            /// <param name="encoding">编码格式</param>
    
            /// <returns>T</returns>
    
            public static T JsonToObject<T>(string json, Encoding encoding)
    
            {
    
                T resultObject = default(T);
    
                try
    
                {
    
                    resultObject = Activator.CreateInstance<T>();
    
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(encoding.GetBytes(json)))
    
                    {
    
                        resultObject = (T)serializer.ReadObject(ms);
    
                    }
    
                }
    
                catch { }
    
                return resultObject;
    
            }
    
            #endregion
    
        }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    {
        "Table": [
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0001",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "生活照料",
                "sort": 0,
                "parent_code": "0000",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0002",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "家政服务",
                "sort": 0,
                "parent_code": "0000",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0003",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "修配服务",
                "sort": 0,
                "parent_code": "0000",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0004",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "医疗服务",
                "sort": 0,
                "parent_code": "0000",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0005",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "资讯服务",
                "sort": 0,
                "parent_code": "0000",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0006",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "文体娱乐",
                "sort": 0,
                "parent_code": "0000",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0007",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "代缴费",
                "sort": 0,
                "parent_code": "0001",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0008",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "代购物",
                "sort": 0,
                "parent_code": "0001",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0009",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "助浴",
                "sort": 0,
                "parent_code": "0001",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0010",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "理发",
                "sort": 0,
                "parent_code": "0001",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0011",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "修脚",
                "sort": 0,
                "parent_code": "0001",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0012",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "打扫卫生",
                "sort": 0,
                "parent_code": "0002",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0013",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "清洗厨房用具",
                "sort": 0,
                "parent_code": "0002",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0014",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "擦玻璃,窗框,窗纱等",
                "sort": 0,
                "parent_code": "0002",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0015",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "清理床上用品及衣服等",
                "sort": 0,
                "parent_code": "0002",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0016",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "钟点工",
                "sort": 0,
                "parent_code": "0002",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0017",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "上门做饭",
                "sort": 0,
                "parent_code": "0002",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0018",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "加点维修",
                "sort": 0,
                "parent_code": "0003",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0019",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "上下水管道维修",
                "sort": 0,
                "parent_code": "0003",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0020",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "门窗维修",
                "sort": 0,
                "parent_code": "0003",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0021",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "开锁,换镜",
                "sort": 0,
                "parent_code": "0003",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0022",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "水池,浴缸,坐便器维修",
                "sort": 0,
                "parent_code": "0003",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0023",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "提醒和监督按时吃药",
                "sort": 0,
                "parent_code": "0004",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0024",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "陪同就医取药",
                "sort": 0,
                "parent_code": "0004",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0025",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "简单体检",
                "sort": 0,
                "parent_code": "0004",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0026",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "推拿按摩等",
                "sort": 0,
                "parent_code": "0004",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0027",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "法律咨询",
                "sort": 0,
                "parent_code": "0005",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0028",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "健康咨询",
                "sort": 0,
                "parent_code": "0005",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0029",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "心里咨询",
                "sort": 0,
                "parent_code": "0005",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0030",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "精神慰藉",
                "sort": 0,
                "parent_code": "0005",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0031",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "棋牌",
                "sort": 0,
                "parent_code": "0006",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0032",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "歌舞",
                "sort": 0,
                "parent_code": "0006",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0033",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "老年网吧",
                "sort": 0,
                "parent_code": "0006",
                "event_day": 0,
                "remark": "0"
            },
            {
                "created_date": "1990-04-04T00:00:00",
                "created_by": "01      ",
                "code": "0034",
                "flag_del": "0",
                "flag_display": "0",
                "name_c": "体育活动",
                "sort": 0,
                "parent_code": "0006",
                "event_day": 0,
                "remark": "0"
            }
        ]
    }


    想把这个json字符串生成一棵树, 解析当前的Json字符串读入树形图控件,什么控件阿?

    please verify my account


    • 已编辑 lctk 2016年4月21日 1:37
    2016年4月21日 1:33
  • Hi lctk,

    >>想把这个json字符串生成一棵树, 解析当前的Json字符串读入树形图控件,什么控件阿?

    如果只是变成树形的话,随便一个json解析工具都可以,当然前提是你的json语句正确(你可以百度json)。 树形图控件 treeview。

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年4月22日 1:26
  • 如果只是变成树形的话,随便一个json解析工具都可以

    没找到啊,只有校验功能,不能解析成树。


    please verify my account

    2016年4月22日 2:08
  • Hi lctk,
    你可以参考下下面的链接:
    json转树状结构(js)
    注:如果是想要生成目录导航树,那个需要搭配框架。
    Regards,
    Moonlight

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年4月22日 6:22
  • 什么框架?

    please verify my account

    2016年4月22日 6:27
  • Hi,

    网站项目的框架。

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年4月26日 6:23