0

I want to deserialize this json file into a native list or array so can I use it in my code. I tried many ways but every time I get different errors, I included the latest one here, please provide my the right way to deserialize the json in the mentioned url.

Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WebRequest.myRequest4+TseReport]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object."

 class Program
            {
        args = myRequest4.TseGet().Result;
        }
           
    
     public class TseReport
        {
            public string t { get; set; }
            public string title { get; set; }
            public string des { get; set; }
            public string l { get; set; }
            public string w { get; set; }
            public string img { get; set; }
        }
        public async static Task<string[]> TseGet()
        {
           
            string[] str = new string[2];
            Uri uri = new Uri("https://tse.ir/news/newsTag/tag_a1.json?_=1604038960668");
            HttpClientHandler handler = new HttpClientHandler();
            List<TseReport> model = null;
            HttpClient client = new HttpClient();
            try
            {
                var task = client.GetAsync(uri)
                  .ContinueWith((taskwithresponse) =>
           {
               var response = taskwithresponse.Result;
               var jsonString = response.Content.ReadAsStringAsync();
               jsonString.Wait();
               model = JsonConvert.DeserializeObject<List<TseReport>>(jsonString.Result);
         
           });
                task.Wait();
                Thread.Sleep(50);
            }
            catch (Exception ex)
            {
                string err = ex.GetBaseException().Message;
                str[1] = err;
            }
            Console.Write(model);
            Console.WriteLine("finished");
            return str;
        }
Stefan
  • 17,448
  • 11
  • 60
  • 79
yasharov
  • 113
  • 10
  • Where is the JSON? – Stefan Oct 31 '20 at 08:48
  • change your model to : public class NewsData { public string t { get; set; } public string title { get; set; } public string des { get; set; } public string l { get; set; } public string w { get; set; } public string img { get; set; } } public class Root { public List newsData { get; set; } public string tn { get; set; } } – Morteza Asadi Oct 31 '20 at 09:08
  • useage : var newsReport = JsonConvert.DeserializeObject(jsonString.Result); – Morteza Asadi Oct 31 '20 at 09:09
  • @MortezaAsadi Thanks for your answer, you mean that the List I am looking for is inside another list, so I should use a list structure to fit that? – yasharov Oct 31 '20 at 09:27
  • Yes, because this post closed, I can't post an answer. Only copy my models and Deserialize JSON string to Root class. This code works perfectly. – Morteza Asadi Oct 31 '20 at 10:33
  • this is my test https://dotnetfiddle.net/v6W8xu – Morteza Asadi Oct 31 '20 at 10:41
  • Sure, I am using your code, and everything is going nice. – yasharov Oct 31 '20 at 11:03

1 Answers1

1

Your problem is described in the error:

Your JSON is an object with an array property:

{
    newsData: [{},{}]
}

To achieve the deserialization as you did, you'd need an object like this:

 [
   {},{},{},...,{}
 ]

So create new C# object like this:

public class NewsData {
    public List<TseReport> newsData;
}

And in your code:

 var newsReport = JsonConvert.DeserializeObject<NewsData>(jsonString.Result);

You can now find your list in newsReport.newsData property.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Well, nice XD .. – Stefan Oct 31 '20 at 08:50
  • Dear Athanasios, Thanks for your answer, It solved my problem, How could I know that I should use the type you mentioned? why a variable should be in brackets? I don't get the necessity? – yasharov Oct 31 '20 at 09:17
  • {} -> means it is a json object, while [] means it's a json collection. So {type:[]} means an object with a property type that is a collection. Hope this helps. Don't forget to upvote accept helpful answers. – Athanasios Kataras Oct 31 '20 at 09:43