首页 > 解决方案 > 获取 json 文件时遇到的错误

问题描述

我需要在远程服务器上拉一个 json 文件。我把文件的链接留在这里。turkmedya.com.tr/anasayfa.json

我还将 json 文件的开放版本作为照片分享。在此处输入图像描述

我还必须从文件中提取类别。你能帮我个忙吗?

Anasayfa.cs

 public class data
    {
        public string sectionType { get; set; }
        public string titleBgColor { get; set; }
        public IList<veri> itemList { get; set; }
      
    }
    public class veri
    {
        public string shortText { get; set; }
        public string fullPath { get; set; }
        public string itemId { get; set; }
        public string imageUrl { get; set; }
    }

    public class cat
    {
        public string categoryId { get; set; }
        public string Title{ get; set; }
    }
    public class ListJson
    {
        public string errorMessage { get; set; }
        public IList<data> data { get; set; }
        public IList<cat> category { get; set; }
    }

HomeController.cs
   public ActionResult Index()
        {
            string url = @"http://turkmedya.com.tr/anasayfa.json";
            string jsonVerisi = "";
            try
            {
                using (WebClient response = new WebClient())
                {
                    jsonVerisi = response.DownloadString(url);
                }
                ListJson account = JsonConvert.DeserializeObject<ListJson>(jsonVerisi);

                foreach (var list in account.data)
                {
                    System.Console.WriteLine(list.ToString());
                    ViewBag.ca = list.itemList.ToList();
                    var a = account.data.Count;
                    var b = list.itemList.Count;
                }

              
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return View();

        }

标签: asp.netjsonxmlasp.net-mvc

解决方案


推荐阅读