json格式问题

Lbulang 2013-11-04 01:12:28
树形表三字段
id parentId text

得到如下json格式:
后台怎么写啊?拼了半天都不对 。 树是无限级的 。
var treeNodes = [
{ "id": "1", "text": "电脑整机", "parentid": "0", "children": [
{ "id": "2", "text": "笔记本", "parentid": "1", "children": [
{ "id": "31", "text": "SONY", "parentid": "2", "children": [] },
{ "id": "23", "text": "LENOVO", "parentid": "2", "children": [] },
{ "id": "25", "text": "IBM", "parentid": "2", "children": [] },
{ "id": "26", "text": "宏基", "parentid": "2", "children": [] },
{ "id": "27", "text": "联想", "parentid": "2", "children": [] },
{ "id": "28", "text": "联想2", "parentid": "2", "children": [] }
]
},
{ "id": "3", "text": "上网本", "parentid": "1", "children": [] },
{ "id": "4", "text": "平板电脑", "parentid": "1", "children": [] },
{ "id": "5", "text": "台式机", "parentid": "1", "children": [] },
{ "id": "6", "text": "服务器", "parentid": "1", "children": [] }
]
},
{ "id": "7", "text": "电脑配件", "parentid": "0", "children": [
{ "id": "8", "text": "CPU", "parentid": "7", "children": [] },
{ "id": "9", "text": "主板", "parentid": "7", "children": [] },
{ "id": "10", "text": "显卡", "parentid": "7", "children": [] },
{ "id": "11", "text": "硬盘", "parentid": "7", "children": [] },
{ "id": "12", "text": "内存", "parentid": "7", "children": [] },
{ "id": "13", "text": "机箱", "parentid": "7", "children": [] },
{ "id": "14", "text": "电源", "parentid": "7", "children": [] },
{ "id": "15", "text": "显示器", "parentid": "7", "children": [] },
{ "id": "16", "text": "刻录机/光驱", "parentid": "7", "children": [] },
{ "id": "17", "text": "声卡", "parentid": "7", "children": [] },
{ "id": "18", "text": "扩展卡", "parentid": "7", "children": [] }
]
},
{ "id": "32", "text": "手机", "parentid": "0", "children": [
{
"id": "33", "text": "诺基亚", "parentid": "32", "children": [
{ "id": "34", "text": "华为", "parentid": "32", "children": [] }
]
},
{ "id": "34", "text": "华为", "parentid": "32", "children": [] },
{ "id": "34", "text": "三星", "parentid": "32", "children": [] },
{ "id": "34", "text": "小米", "parentid": "32", "children": [] },
{ "id": "34", "text": "苹果", "parentid": "32", "children": [] }
]
},
{ "id": "5", "text": "篮球", "parentId": "1", "children": [] }
];


众位高手帮忙一下啊
...全文
236 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuan.ye 2013-11-05
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public partial class _Default : System.Web.UI.Page
{
    private string jsonString = @"[
        { 'id': '1', 'text': '电脑整机', 'parentid': '0', 'children': [
    { 'id': '2', 'text': '笔记本', 'parentid': '1', 'children': [
{ 'id': '31', 'text': 'SONY', 'parentid': '2', 'children': [] },
{ 'id': '23', 'text': 'LENOVO', 'parentid': '2', 'children': [] },
{ 'id': '25', 'text': 'IBM', 'parentid': '2', 'children': [] },
{ 'id': '26', 'text': '宏基', 'parentid': '2', 'children': [] },
{ 'id': '27', 'text': '联想', 'parentid': '2', 'children': [] },
{ 'id': '28', 'text': '联想2', 'parentid': '2', 'children': [] }
   ]
    },
{ 'id': '3', 'text': '上网本', 'parentid': '1', 'children': [] },
{ 'id': '4', 'text': '平板电脑', 'parentid': '1', 'children': [] },
{ 'id': '5', 'text': '台式机', 'parentid': '1', 'children': [] },
{ 'id': '6', 'text': '服务器', 'parentid': '1', 'children': [] }
]
        },
   { 'id': '7', 'text': '电脑配件', 'parentid': '0', 'children': [
{ 'id': '8', 'text': 'CPU', 'parentid': '7', 'children': [] },
{ 'id': '9', 'text': '主板', 'parentid': '7', 'children': [] },
{ 'id': '10', 'text': '显卡', 'parentid': '7', 'children': [] },
{ 'id': '11', 'text': '硬盘', 'parentid': '7', 'children': [] },
{ 'id': '12', 'text': '内存', 'parentid': '7', 'children': [] },
{ 'id': '13', 'text': '机箱', 'parentid': '7', 'children': [] },
{ 'id': '14', 'text': '电源', 'parentid': '7', 'children': [] },
{ 'id': '15', 'text': '显示器', 'parentid': '7', 'children': [] },
{ 'id': '16', 'text': '刻录机/光驱', 'parentid': '7', 'children': [] },
{ 'id': '17', 'text': '声卡', 'parentid': '7', 'children': [] },
{ 'id': '18', 'text': '扩展卡', 'parentid': '7', 'children': [] }
]
   },
    { 'id': '32', 'text': '手机', 'parentid': '0', 'children': [
  {
      'id': '33', 'text': '诺基亚', 'parentid': '32', 'children': [
                                  { 'id': '34', 'text': '华为', 'parentid': '32', 'children': [] }
                                  ]
  },
                              { 'id': '34', 'text': '华为', 'parentid': '32', 'children': [] },
                              { 'id': '34', 'text': '三星', 'parentid': '32', 'children': [] },
                              { 'id': '34', 'text': '小米', 'parentid': '32', 'children': [] },
                              { 'id': '34', 'text': '苹果', 'parentid': '32', 'children': [] }
                                                                 	]
    },
                            { 'id': '5', 'text': '篮球', 'parentId': '1', 'children': [] }
];";

    protected void Page_Load(object sender, EventArgs e)
    {
        treeNode rootNode = new treeNode();
        rootNode.id="1";
        rootNode.text="电脑整机";
        rootNode.parentid="0";
        
        treeNode childNode1 = new treeNode();
        childNode1.id="2";
        childNode1.text="笔记本";
        childNode1.parentid="1";

        treeNode childNode2 = new treeNode();
        childNode2.id="3";
        childNode2.text="SONY";
        childNode2.parentid="1";

        if (rootNode.children == null)
            rootNode.children = new List<treeNode>();

        rootNode.children.Add(childNode1);
        rootNode.children.Add(childNode2);

        string json = JsonConvert.SerializeObject(rootNode, Formatting.Indented);
    }

    /// <summary>
    /// 树结构(根据js插件定义的数据结构)
    /// </summary>
    public class treeNode
    {
        private string _id;

        private string _text;

        private string _parentid;

        private List<treeNode> _children;

        public string id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string text
        {
            get { return _text; }
            set { _text = value; }
        }
        public string parentid
        {
            get { return _parentid; }
            set { _parentid = value; }
        }
        public List<treeNode> children
        {
            get { return _children; }
            set { _children = value; }
        }
    }
}
Lbulang 2013-11-05
  • 打赏
  • 举报
回复
引用 12 楼 leujunbao 的回复:
楼主去补补基础吧,楼上说的很清楚了,无法解释了
是要补了 自己补没有自己去去问记得牢啊
leujunbao 2013-11-05
  • 打赏
  • 举报
回复
楼主去补补基础吧,楼上说的很清楚了,无法解释了
Lbulang 2013-11-05
  • 打赏
  • 举报
回复
引用 10 楼 yeness 的回复:
递归你懂吗?如果不懂,就没着了
是不是类似下面这个 private string CreateJsonData(DataTable dt, string jsonStr, string id) { string mId = string.Empty; DataRow[] ChildRows = dt.Select(string.Format("ParentId='{0}'", id)); if (ChildRows.Length > 0) { jsonStr += ","; jsonStr += string.Format(" \"menus\":[ "); foreach (DataRow childRow in ChildRows) { jsonStr += "{"; string text= childRow["text"].ToString(); string parentId = childRow["ParentId"].ToString(); jsonStr += string.Format("\"id\":\"{0}\",\"text\":\"{1}\",\"ParentId\":\"{2}\",\"", childRow["id"].ToString(), childRow["text"].ToString(), parentId); jsonStr += "},"; CreateJsonData(dt, jsonStr, childRow["id"].ToString()); } if (jsonStr.LastIndexOf(",") > 0) jsonStr = jsonStr.Remove(jsonStr.LastIndexOf(",") - 1); if (ChildRows != null && ChildRows.Length > 0) jsonStr += "}]"; } return jsonStr; } private string GetTreeStr() { DataRow[] RootRows = null; string jsonStr = string.Empty; DataTable dt = bll.GetInvCategoryRelationList("").Tables[0]; if (RootRows != null && RootRows.Length > 0) jsonStr += "{ \"menus\":[ "; foreach (DataRow row in dt.Rows) { string ParentId = row["ParentId"].ToString(); string id= row["id"].ToString(); string text= row["text"].ToString(); jsonStr += "{"; jsonStr += string.Format("\"id\":\"{0}\",\"text\":\"{1}\",\"ParentId\":\"{2}\"", id, text, ParentId); jsonStr = CreateJsonData(dt, jsonStr, id); jsonStr += "},"; } if (jsonStr.LastIndexOf(",") > 0) jsonStr = jsonStr.Remove(jsonStr.LastIndexOf(",") - 1); if (RootRows != null && RootRows.Length > 0) jsonStr += "}]}"; return jsonStr; }
xuan.ye 2013-11-05
  • 打赏
  • 举报
回复
递归你懂吗?如果不懂,就没着了
Lbulang 2013-11-05
  • 打赏
  • 举报
回复
引用 8 楼 yeness 的回复:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public partial class _Default : System.Web.UI.Page
{
    private string jsonString = @"[
        { 'id': '1', 'text': '电脑整机', 'parentid': '0', 'children': [
    { 'id': '2', 'text': '笔记本', 'parentid': '1', 'children': [
{ 'id': '31', 'text': 'SONY', 'parentid': '2', 'children': [] },
{ 'id': '23', 'text': 'LENOVO', 'parentid': '2', 'children': [] },
{ 'id': '25', 'text': 'IBM', 'parentid': '2', 'children': [] },
{ 'id': '26', 'text': '宏基', 'parentid': '2', 'children': [] },
{ 'id': '27', 'text': '联想', 'parentid': '2', 'children': [] },
{ 'id': '28', 'text': '联想2', 'parentid': '2', 'children': [] }
   ]
    },
{ 'id': '3', 'text': '上网本', 'parentid': '1', 'children': [] },
{ 'id': '4', 'text': '平板电脑', 'parentid': '1', 'children': [] },
{ 'id': '5', 'text': '台式机', 'parentid': '1', 'children': [] },
{ 'id': '6', 'text': '服务器', 'parentid': '1', 'children': [] }
]
        },
   { 'id': '7', 'text': '电脑配件', 'parentid': '0', 'children': [
{ 'id': '8', 'text': 'CPU', 'parentid': '7', 'children': [] },
{ 'id': '9', 'text': '主板', 'parentid': '7', 'children': [] },
{ 'id': '10', 'text': '显卡', 'parentid': '7', 'children': [] },
{ 'id': '11', 'text': '硬盘', 'parentid': '7', 'children': [] },
{ 'id': '12', 'text': '内存', 'parentid': '7', 'children': [] },
{ 'id': '13', 'text': '机箱', 'parentid': '7', 'children': [] },
{ 'id': '14', 'text': '电源', 'parentid': '7', 'children': [] },
{ 'id': '15', 'text': '显示器', 'parentid': '7', 'children': [] },
{ 'id': '16', 'text': '刻录机/光驱', 'parentid': '7', 'children': [] },
{ 'id': '17', 'text': '声卡', 'parentid': '7', 'children': [] },
{ 'id': '18', 'text': '扩展卡', 'parentid': '7', 'children': [] }
]
   },
    { 'id': '32', 'text': '手机', 'parentid': '0', 'children': [
  {
      'id': '33', 'text': '诺基亚', 'parentid': '32', 'children': [
                                  { 'id': '34', 'text': '华为', 'parentid': '32', 'children': [] }
                                  ]
  },
                              { 'id': '34', 'text': '华为', 'parentid': '32', 'children': [] },
                              { 'id': '34', 'text': '三星', 'parentid': '32', 'children': [] },
                              { 'id': '34', 'text': '小米', 'parentid': '32', 'children': [] },
                              { 'id': '34', 'text': '苹果', 'parentid': '32', 'children': [] }
                                                                 	]
    },
                            { 'id': '5', 'text': '篮球', 'parentId': '1', 'children': [] }
];";

    protected void Page_Load(object sender, EventArgs e)
    {
        treeNode rootNode = new treeNode();
        rootNode.id="1";
        rootNode.text="电脑整机";
        rootNode.parentid="0";
        
        treeNode childNode1 = new treeNode();
        childNode1.id="2";
        childNode1.text="笔记本";
        childNode1.parentid="1";

        treeNode childNode2 = new treeNode();
        childNode2.id="3";
        childNode2.text="SONY";
        childNode2.parentid="1";

        if (rootNode.children == null)
            rootNode.children = new List<treeNode>();

        rootNode.children.Add(childNode1);
        rootNode.children.Add(childNode2);

        string json = JsonConvert.SerializeObject(rootNode, Formatting.Indented);
    }

    /// <summary>
    /// 树结构(根据js插件定义的数据结构)
    /// </summary>
    public class treeNode
    {
        private string _id;

        private string _text;

        private string _parentid;

        private List<treeNode> _children;

        public string id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string text
        {
            get { return _text; }
            set { _text = value; }
        }
        public string parentid
        {
            get { return _parentid; }
            set { _parentid = value; }
        }
        public List<treeNode> children
        {
            get { return _children; }
            set { _children = value; }
        }
    }
}
数据来自 DataTable foreach怎么添加到集合里面去啊 DataRow[] RootRows = null; RootRows = dt.Select(string.Format("ParentId='{0}' ", Guid.Empty.ToString()), parentId); Nodestr += "["; foreach (DataRow row in RootRows) { string parentid= row["ParentId"].ToString(); string id= row["id"].ToString(); string text= row["text"].ToString(); ..........里面不同级 咋弄啊 }
Lbulang 2013-11-04
  • 打赏
  • 举报
回复
引用 4 楼 yeness 的回复:
http://json.codeplex.com/ 1、先下载这个dll 引入到项目中; 2、根据前台控件属性,编写 formbean 类的数据结构。 3、递归你的数据,装入一个 list<formbean类的> 的集合中; 4、用 gson 对象=new gson(); string jsonString= 对象.方法(list<formbean类的> 对象).tostring(); jsonstring 就是你想要的了。当然你可以看gson的官方demo和文档
你说的这个应该是可以,但是我还是不知道怎么搞啊 能否多指教啊 谢谢啊
Lbulang 2013-11-04
  • 打赏
  • 举报
回复
引用 4 楼 yeness 的回复:
http://json.codeplex.com/ 1、先下载这个dll 引入到项目中; 2、根据前台控件属性,编写 formbean 类的数据结构。 3、递归你的数据,装入一个 list<formbean类的> 的集合中; 4、用 gson 对象=new gson(); string jsonString= 对象.方法(list<formbean类的> 对象).tostring(); jsonstring 就是你想要的了。当然你可以看gson的官方demo和文档
下载下来一大堆啊 看那个文件啊?还有 是哪一个dll啊?
Lbulang 2013-11-04
  • 打赏
  • 举报
回复
引用 4 楼 yeness 的回复:
http://json.codeplex.com/ 1、先下载这个dll 引入到项目中; 2、根据前台控件属性,编写 formbean 类的数据结构。 3、递归你的数据,装入一个 list<formbean类的> 的集合中; 4、用 gson 对象=new gson(); string jsonString= 对象.方法(list<formbean类的> 对象).tostring(); jsonstring 就是你想要的了。当然你可以看gson的官方demo和文档
晚上 回去试试先
xuan.ye 2013-11-04
  • 打赏
  • 举报
回复
http://json.codeplex.com/ 1、先下载这个dll 引入到项目中; 2、根据前台控件属性,编写 formbean 类的数据结构。 3、递归你的数据,装入一个 list<formbean类的> 的集合中; 4、用 gson 对象=new gson(); string jsonString= 对象.方法(list<formbean类的> 对象).tostring(); jsonstring 就是你想要的了。当然你可以看gson的官方demo和文档
Lbulang 2013-11-04
  • 打赏
  • 举报
回复
引用 2 楼 yeness 的回复:
google 的 json 有一个 .net版本,下载; 你的前台应该是ext 或者 jquery 的插件吧; 后台只要定义一个formbean public class treeNode { private string id; private string parentid; private List<treeNode> children; } 把你的数据循环放在 list<treeNode> treelist 里面 用gson的构造方法(treeList).ToString() 就是你要的结果
这位仁兄 可否再详细点啊
xuan.ye 2013-11-04
  • 打赏
  • 举报
回复
google 的 json 有一个 .net版本,下载; 你的前台应该是ext 或者 jquery 的插件吧; 后台只要定义一个formbean public class treeNode { private string id; private string parentid; private List<treeNode> children; } 把你的数据循环放在 list<treeNode> treelist 里面 用gson的构造方法(treeList).ToString() 就是你要的结果
md5e 2013-11-04
  • 打赏
  • 举报
回复
用LitJson来实现吧

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧