5

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.

         {  "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

[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.

2
  • 1
    You need to create a new class name ShowInfo and add Array or List of it in the NowShowingInfo. The class ShowInfo will contain properties name: ShowId and StartTime Commented Aug 7, 2013 at 7:45
  • 1
    You need to create a new class name ShowInfo and add Array or List of it in the NowShowingInfo. The class ShowInfo will con Commented Apr 2, 2014 at 12:45

2 Answers 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

Sign up to request clarification or add additional context in comments.

3 Comments

how can i convert my NowShowingInfo into your NowShowingInfo obj.NowShowingGetList(ShowDate) return NowShowingInfo of my type
newbie means @NisargShah
Ok. First of all your obj.NowShowingGetList(ShowDate) will now return only 1 value for 1 movie. the Object of NowshowingInfo will contain only one value for 1 movie. and the show times will be stored in list. I.E. ShowInformation.Add(ShowInfoObject)
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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.