【问题标题】:how to get the nested JSON objects from SQL table in ASP.Net Web API如何从 ASP.Net Web API 中的 SQL 表中获取嵌套的 JSON 对象
【发布时间】:2020-10-04 09:26:24
【问题描述】:

我需要通过 asp.Net Web API 提供下面提到的 JSON 输出。 此输出将用于以角度显示组织层次结构(树视图)。

我在 SQL 表中有员工数据如下:

EMP_NO  EMP_NAME  DESIGNATION  Manager_ID

所需的 JSON 输出:

 myobject= {
      "parent":[
        {
          "name":"radha Acharya",
          "designation":"General Counsel",
          "state":"",
          "active":false
        },
        {
          "name":"Rakesh mishra",
          "designation":"General Counse2",
          "state":"",
          "active":false
        },
        {
          "name":"Suri patil",
          "designation":"General Counse3",
          "state":"",
          "active":true
        }
      ],
      "childs":[
        {
          "name":"child 1",
          "designation":"Deputy General Counsel",
          "state":"",
          "active":false,
          "hasChilds":true
        },
        {
          "name":"child 2",
          "designation":"Deputy General Counsel",
          "state":"",
          "active":false,
          "hasChilds":true
        },
        {
          "name":"child 3",
          "designation":"Deputy General Counsel",
          "state":"",
          "active":false,
          "hasChilds":false
        },
        {
          "name":"child 4",
          "designation":"Deputy General Counsel",
          "state":"",
          "active":false,
          "hasChilds":true
        },
        {
          "name":"child 5",
          "designation":"Deputy General Counsel",
          "state":"Resigned",
          "active":false,
          "hasChilds":true
        },
        {
          "name":"child 6",
          "designation":"Deputy General Counsel",
          "state":"",
          "active":false,
          "hasChilds":true
        }
      ]
    }

在角度它会像这样显示 Front end display image

【问题讨论】:

  • 你将如何从 JSON 输出中确定谁是谁的孩子?

标签: c# sql asp.net-mvc asp.net-web-api webapi


【解决方案1】:

不确定您打算如何使用 Json,因为父列表和子列表之间没有联系。但您可以通过以下方式从 SQL Server 获取 JSON:

#创建parentchild类型的类

#使用两个数组/列表属性创建一个名为myobject 的对象 - parentchilds

#查询 SQL DB 并填充 myobject 对象

#将myobject对象转换为JSON并从API动作发送

public class parent
{
    public string name { get; set; } 
    public string designation { get; set; } 
    public string state { get; set; } 
    public bool active { get; set; }
}

public class child
{
    public string name { get; set; } 
    public string designation { get; set; } 
    public string state { get; set; } 
    public bool active { get; set; } 
    public bool hasChilds { get; set; }
}

public class myboject
{
    public List<Parent> parent { get; set; } 
    public List<Child> childs { get; set; }
}

myboject myboj = new myboject();
myboj.parent = new List<Parent>();
myboj.child = new List<Child>();

//查询您的 sql server 数据库并使用适当的数据填充 myboj.parentmyboj.child

然后做Json转换:

return Newtonsoft.Json.JsonConvert.SerializeObject(myboj);

这会将myObj 转换为json 对象并返回。

【讨论】:

  • 感谢 Rahatur,现在我对这个实现有了一个想法,感谢您的努力。
  • @Virendra 很高兴它有帮助。希望您不介意接受答案并投票!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多