Share via


Serialize IEnumerable to Json

Question

Wednesday, April 15, 2015 10:00 AM

Hi,

I'm trying to convert a IEnumerable to json but get the message: Cannot implicitly convert type 'string' to 'System.Web.Mvc.JsonResult.

What to do?

Thanx!

public JsonResult LoadRssLinks(string link)
        {           
            var rssList = new RenderRSS().GetRSS(link);
            var jsonList = new JavaScriptSerializer().Serialize(rssList);
            return jsonList;
        }

 public class RSSitem
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string PublishDate { get; set; }
        public string Category { get; set; }
        public string Link { get; set; }        
    }

public IEnumerable<RSSitem> GetRSS(string rssUrl)
        {
            var rssFeed = XDocument.Load(rssUrl);
            var rssFeedOut = from item in rssFeed.Descendants("item")
                             select new RSSitem
                             {
                                 Title = HttpUtility.HtmlEncode(item.Element("title").Value),
                                 PublishDate = HttpUtility.HtmlEncode(item.Element("pubDate").Value),
                                 Category = HttpUtility.HtmlEncode(item.Element("category").Value),
                                 Link = HttpUtility.HtmlEncode(item.Element("link").Value),                                
                                 Description = GetDescription(item.Element("description").Value)
                             };            
            return rssFeedOut.OrderBy(x => x.PublishDate);

        }

All replies (4)

Thursday, April 16, 2015 1:41 AM âś…Answered

Hello AndyIsTaken,

Actually Json Helper accepts Object type to the parameter. So you don't need to use JavaScriptSerializer at all the Helper method will serialize the object for you. Please change your method as below and check if it works.

public JsonResult LoadRssLinks(string link)
{           
  var rssList = new RenderRSS().GetRSS(link);
  return Json(rssList);
}

and change return type of the GetRss to List. List is also Ienumarable.

public List<RSSitem> GetRSS(string rssUrl)
        {
            var rssFeed = XDocument.Load(rssUrl);
            var rssFeedOut = from item in rssFeed.Descendants("item")
                             select new RSSitem
                             {
                                 Title = HttpUtility.HtmlEncode(item.Element("title").Value),
                                 PublishDate = HttpUtility.HtmlEncode(item.Element("pubDate").Value),
                                 Category = HttpUtility.HtmlEncode(item.Element("category").Value),
                                 Link = HttpUtility.HtmlEncode(item.Element("link").Value),                                
                                 Description = GetDescription(item.Element("description").Value)
                             };            
            return rssFeedOut.OrderBy(x => x.PublishDate).ToList();

        }

Hope this helps.

With Regards,

Krunal Parekh


Wednesday, April 15, 2015 2:34 PM

Try

return Json(jsonList, JsonRequestBehavior.AllowGet); //Here jsonList should be of type string .

See MSDN reference: https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult%28v=vs.118%29.aspx

Please note, jsonList should be string then, it would work.

Basically the Json helper expects the parameter to be passed as a string.


Thursday, April 16, 2015 3:19 AM

Tank you Siva!


Thursday, April 16, 2015 3:20 AM

Thank you Krunal, this solved my problem.