转换Json格式响应从webservice

时间:2021-11-01 14:05:19

my web-service return json data as given below but i wanted is like as in 2nd CodeSnipts. my web-service and class as given below.

我的web服务返回的json数据如下所示,但我想要的是第二个CodeSnipts。我的web服务和类如下所示。

         {  "ShowID": 10107,
            "StartTime": "3:00 PM",
            "MovieID": 13,
            "Movie": "Bhaag Milkha Bhaag ",
            "Screen": "CDC SCreen2",
            "MediaPath": "bmb1_568962.jpg"
         },{  "ShowID": 115,
            "StartTime": "6:00 PM",
            "MovieID": 13,
            "Movie": "Bhaag Milkha Bhaag ",
            "Screen": "CDC SCreen2",
            "MediaPath": "bmb1_568962.jpg"
         },{  "ShowID": 110,
            "StartTime": "9:00 PM",
            "MovieID": 13,
            "Movie": "Bhaag Milkha Bhaag ",
            "Screen": "CDC SCreen2",
            "MediaPath": "bmb1_568962.jpg"
         }

but i want as

但我想要

   {
        "MovieID": 13,
        "Movie": "Bhaag Milkha Bhaag ",
        "Screen": "CDC SCreen2",
        "MediaPath": "bmb1_568962.jpg",
        "ShowInfo": [
            {
                "ShowID": 10107,
                "StartTime": "3:00 PM"
            },
            {
                "ShowID": 115,
                "StartTime": "6:00 PM"
            },
            {
                "ShowID": 110,
                "StartTime": "9:00 PM"
            }
        ]
    }

my c# code for Webservice as

我的Webservice的c#代码。

[WebMethod]
public string NowShowingGetList(DateTime ShowDate)
{
    HomeController obj = new HomeController();

    JavaScriptSerializer js = new JavaScriptSerializer();
    string retJSON = js.Serialize(obj.NowShowingGetList(ShowDate));
    return retJSON;
}

class as

public class NowShowingInfo
{
    public int ShowID { get; set; }
    public string StartTime { get; set; }
    public int MovieID { get; set; }
    public string Movie { get; set; }
    public string Screen { get; set; }
    public string MediaPath { get; set; }
}

here obj.NowShowingGetList(ShowDate) return List Thankyou in advance.

现在,显示getlist (ShowDate)返回列表,谢谢。

2 个解决方案

#1


2  

Infos are as

信息是

 public class ShowInfo
    {
        public int ShowID { get; set; }
        public string StartTime { get; set; }
    }
    public class NowShowing
    {
        public List<ShowInfo> ShowInfo { get; set; }
        public int MovieID { get; set; }
        public string Movie { get; set; }
        public string Screen { get; set; }
        public string MediaPath { get; set; }
    }

and

public List<NowShowing> NowShowingGetList(DateTime ShowDate)
    {            
        List<NowShowingInfo> objshowList = obj.NowShowingGetList(ShowDate);

        int movieID = 0;
        List<NowShowing> objShowingList = new List<NowShowing>();

        NowShowing obj2 = new NowShowing();
        ShowInfo objshowInfo = new ShowInfo();
        List<ShowInfo> objshowInfoList = new List<ShowInfo>();
        int count = 0;
        string Screen="";
        foreach (NowShowingInfo objs in objshowList)
        {
            if (movieID != objs.MovieID )
            {
                if (count != 0)
                {
                    obj2.ShowInfo = objshowInfoList;
                    objshowInfoList = new List<ShowInfo>();
                    objShowingList.Add(obj2);
                    count = 0;
                }
                obj2 = new NowShowing();
                obj2.MovieID = objs.MovieID;
                obj2.Movie = objs.Movie;
                obj2.Screen = objs.Screen;
                obj2.MediaPath = objs.MediaPath;

                if (count == 0)
                {
                    objshowInfo = new ShowInfo();
                    objshowInfo.ShowID = objs.ShowID;
                    objshowInfo.StartTime = objs.StartTime;
                    objshowInfoList.Add(objshowInfo);
                }

            }
            else
            {
                objshowInfo = new ShowInfo();
                objshowInfo.ShowID = objs.ShowID;
                objshowInfo.StartTime = objs.StartTime;
                objshowInfoList.Add(objshowInfo);
            }
            movieID = objs.MovieID;
            Screen = objs.Screen;
            count++;
        }

        obj2.ShowInfo = objshowInfoList;
        objShowingList.Add(obj2);
        return objShowingList;
    }

#2


5  

Make changes as per the entity

根据实体进行更改

public class NowShowingInfo
{
    public List<ShowInfo> ShowInformation { get; set; }
    public int MovieID { get; set; }
    public string Movie { get; set; }
    public string Screen { get; set; }
    public string MediaPath { get; set; }
}

public class ShowInfo
{
    public int ShowID { get; set; }
    public string StartTime { get; set; }
}

make related changes in your webservice

在您的web服务中进行相关的更改

#1


2  

Infos are as

信息是

 public class ShowInfo
    {
        public int ShowID { get; set; }
        public string StartTime { get; set; }
    }
    public class NowShowing
    {
        public List<ShowInfo> ShowInfo { get; set; }
        public int MovieID { get; set; }
        public string Movie { get; set; }
        public string Screen { get; set; }
        public string MediaPath { get; set; }
    }

and

public List<NowShowing> NowShowingGetList(DateTime ShowDate)
    {            
        List<NowShowingInfo> objshowList = obj.NowShowingGetList(ShowDate);

        int movieID = 0;
        List<NowShowing> objShowingList = new List<NowShowing>();

        NowShowing obj2 = new NowShowing();
        ShowInfo objshowInfo = new ShowInfo();
        List<ShowInfo> objshowInfoList = new List<ShowInfo>();
        int count = 0;
        string Screen="";
        foreach (NowShowingInfo objs in objshowList)
        {
            if (movieID != objs.MovieID )
            {
                if (count != 0)
                {
                    obj2.ShowInfo = objshowInfoList;
                    objshowInfoList = new List<ShowInfo>();
                    objShowingList.Add(obj2);
                    count = 0;
                }
                obj2 = new NowShowing();
                obj2.MovieID = objs.MovieID;
                obj2.Movie = objs.Movie;
                obj2.Screen = objs.Screen;
                obj2.MediaPath = objs.MediaPath;

                if (count == 0)
                {
                    objshowInfo = new ShowInfo();
                    objshowInfo.ShowID = objs.ShowID;
                    objshowInfo.StartTime = objs.StartTime;
                    objshowInfoList.Add(objshowInfo);
                }

            }
            else
            {
                objshowInfo = new ShowInfo();
                objshowInfo.ShowID = objs.ShowID;
                objshowInfo.StartTime = objs.StartTime;
                objshowInfoList.Add(objshowInfo);
            }
            movieID = objs.MovieID;
            Screen = objs.Screen;
            count++;
        }

        obj2.ShowInfo = objshowInfoList;
        objShowingList.Add(obj2);
        return objShowingList;
    }

#2


5  

Make changes as per the entity

根据实体进行更改

public class NowShowingInfo
{
    public List<ShowInfo> ShowInformation { get; set; }
    public int MovieID { get; set; }
    public string Movie { get; set; }
    public string Screen { get; set; }
    public string MediaPath { get; set; }
}

public class ShowInfo
{
    public int ShowID { get; set; }
    public string StartTime { get; set; }
}

make related changes in your webservice

在您的web服务中进行相关的更改