本文介绍了带有MVC的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要嵌套的json输出,我的代码工作正常如果我从一个表中获取数据但我需要两个链接表之间的数据,例如我需要如下数据:



$ b我的res.cs中有$ b

我有:



 公共课客房

    {

        public string Room_No {get;组; }¥b $ b        public string说明{get;组; }¥b $ b        public string Need_Confirmation {get;组; }¥b $ b        public string Location {get;组; }¥b $ b        public string Status {get;组; }¥b $ b        public int NoShowAfter {get;组; }¥b $ b        public string Resource_Name {get;组; }¥b $ b        public string Full_Path {get;组; }¥b $ b        public string Attached_Path {get;组; }¥b $ b       公共字符串Concern_Email {get;组; }


    }



   公共类Resources_Links

    {

        public string Room_No {get;组; }¥b $ b        public string Name {get;组; }¥b $ b        public string Link {get;组; }




    }



好的房间,下面的代码是罚款:


但如何变得像上图:



        //获取所有房间资源

        [HttpGet]

        [ActionName(" GetRoomsWithResources")]
$
  &NBSP; &NBSP; &NBSP;公共列表<房间> GetRoomsWithResources()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string source4 = System.Configuration.ConfigurationManager.ConnectionStrings [" DefaultConnection"]。ConnectionString;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SqlConnection conn4 = new SqlConnection(source4);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SqlCommand cmd4 = new SqlCommand();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SqlDataReader reader4;



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string sql4 =" select a.Room_No,a.Description,a.Need_Confirmation,a.Location,a.Status,a.NoShowAfter,c.Resource_Name,c.Full_Path,c.Attached_Path,c.Concern_Email From Rooms a,Room_Resources b,资源
c其中a.ID = b.Room_ID和b.Resource_ID = c.ID" ;;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; cmd4.CommandText = sql4;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; cmd4.CommandType = CommandType.Text;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; cmd4.Connection = conn4;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;房间emp4 = null;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; conn4.Open();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; reader4 = cmd4.ExecuteReader();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;列表与LT;房间> emps4 =新列表<房间>();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; while(reader4.Read())

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //读取数据

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4 = new Rooms();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Room_No = reader4.GetValue(0)为字符串;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Description = reader4.GetValue(1)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Need_Confirmation = reader4.GetValue(2)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Location = reader4.GetValue(3)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Status = reader4.GetValue(4)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.NoShowAfter = Convert.ToInt32(reader4.GetValue(5));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Resource_Name = reader4.GetValue(6)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Full_Path = reader4.GetValue(7)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Attached_Path = reader4.GetValue(8)as string;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emp4.Concern_Email = reader4.GetValue(9)as string;



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; emps4.Add(emp4);



$
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; conn4.Close();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return emps4;



  &NBSP; &NBSP; &NBSP; }



它们与列ID链接




解决方案

I need nested json output, my code is working fine if i get the data from one table but i need the data between two linked table for example i need the data like the below :

in My res.cs i have :

 public class Rooms
    {
        public string Room_No { get; set; }
        public string Description { get; set; }
        public string Need_Confirmation { get; set; }
        public string Location { get; set; }
        public string Status { get; set; }
        public int NoShowAfter { get; set; }
        public string Resource_Name { get; set; }
        public string Full_Path { get; set; }
        public string Attached_Path { get; set; }
        public string Concern_Email { get; set; }

    }

    public class Resources_Links
    {
        public string Room_No { get; set; }
        public string Name { get; set; }
        public string Link { get; set; }


    }

Ok for Rooms the code below is return fine:

but how to get same like the above picture :

       //Get All Rooms with Resources
        [HttpGet]
        [ActionName("GetRoomsWithResources")]
        public List<Rooms> GetRoomsWithResources()
        {
            string source4 = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection conn4 = new SqlConnection(source4);

            SqlCommand cmd4 = new SqlCommand();
            SqlDataReader reader4;

            string sql4 = "Select a.Room_No,a.Description, a.Need_Confirmation, a.Location, a.Status, a.NoShowAfter , c.Resource_Name, c.Full_Path, c.Attached_Path, c.Concern_Email From Rooms a, Room_Resources b, Resources c Where a.ID = b.Room_ID And b.Resource_ID = c.ID";
            cmd4.CommandText = sql4;
            cmd4.CommandType = CommandType.Text;
            cmd4.Connection = conn4;
            Rooms emp4 = null;
            conn4.Open();
            reader4 = cmd4.ExecuteReader();
            List<Rooms> emps4 = new List<Rooms>();
            while (reader4.Read())
            {
                //read data
                emp4 = new Rooms();
                emp4.Room_No = reader4.GetValue(0) as string;
                emp4.Description = reader4.GetValue(1) as string;
                emp4.Need_Confirmation = reader4.GetValue(2) as string;
                emp4.Location = reader4.GetValue(3) as string;
                emp4.Status = reader4.GetValue(4) as string;
                emp4.NoShowAfter = Convert.ToInt32(reader4.GetValue(5));
                emp4.Resource_Name = reader4.GetValue(6) as string;
                emp4.Full_Path = reader4.GetValue(7) as string;
                emp4.Attached_Path = reader4.GetValue(8) as string;
                emp4.Concern_Email = reader4.GetValue(9) as string;

                emps4.Add(emp4);


            }
            conn4.Close();
            return emps4;

        }

they are link with column ID

解决方案


这篇关于带有MVC的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:25