Converting JSON to Object fails - Cannot deserialize the current JSON object into System.Collections.Generic.List

后端 未结 4 1847
暖寄归人
暖寄归人 2021-01-29 12:48

I\'m using the API of www.textlocal.in, which returns a JSON formatted object.

JSON

{  
   \"warnings\":[  
      {  
         \"messag         


        
相关标签:
4条回答
  • 2021-01-29 12:57

    Based on string you class structure should be like this :

    public class Warning
    {
        public string message { get; set; }
        public string numbers { get; set; }
    }
    
    public class Message
    {
        public int num_parts { get; set; }
        public string sender { get; set; }
        public string content { get; set; }
    }
    
    public class Message2
    {
        public string id { get; set; }
        public long recipient { get; set; }
    }
    
    public class RootObject
    {
        public List<Warning> warnings { get; set; }
        public int balance { get; set; }
        public int batch_id { get; set; }
        public int cost { get; set; }
        public int num_messages { get; set; }
        public Message message { get; set; }
        public string receipt_url { get; set; }
        public string custom { get; set; }
        public List<string> inDND { get; set; }
        public List<Message2> messages { get; set; }
        public string status { get; set; }
    }
    

    It looks like your class structure is not proper, Make use of visual studio and generate C# class from json string and then using that generated class try to deserialize class.

    Read : Visual Studio Generate Class From JSON or XML

    0 讨论(0)
  • 2021-01-29 12:57

    Looks like this is a very old post, still thought of answering.

    First of all, your Json data is singular which means, either

    var a = JsonConvert.DeserializeObject<List<jsonToObj[]>>(richTextBox1.Text);
    

    or

    var a = JsonConvert.DeserializeObject<List<jsonToObj>>(richTextBox1.Text);
    

    may not work for you.

    You can either try:

    var a = JsonConvert.DeserializeObject<jsonToObj>(richTextBox1.Text);
    

    or

    enclose the data with [ and ], which would do the trick.
    

    make sure your parsing single object vs list of objects.

    0 讨论(0)
  • 2021-01-29 13:03

    I simulated your problem and made the following changes that worked:

    1. Change the method that deserializes to this:

      var a = JsonConvert.DeserializeObject<jsonToObj>(richTextBox1.Text);
      

      The result of the JSON you receive is not a List, so it will not work to deserialize to List<>.

    2. The recipient property of the messages class receives values larger than an integer, so it must be transformed into a long like this:

      public long recipient { get; set; }
      

    These changes solve your problem.

    0 讨论(0)
  • 2021-01-29 13:15

    First of all you have to figure out what your API returns.

    Right now you're trying to parse a List of jsonToObj Arrays (List<jsonToObj[]>). You have to decide whether to use a jsonToObj[] or List<jsonToObj> or a simple jsonToObj which your API provides now:

    var a = JsonConvert.DeserializeObject<jsonToObj>(richTextBox1.Text);
    

    But this then throws:

    JSON integer 918819437284 is too large or small for an Int32. Path 'messages[0].recipient', line 25, position 33."

    So make sure you use a Long for that.

    public class messages
    {
        public string id { get; set; }
        public long recipient { get; set; }
    }
    

    Furthermore you can add inDND to your jsonToObj class if you need the info:

    public class jsonToObj
    {
      ...
      public string[] inDND { get; set; }
      ...
    }
    

    0 讨论(0)
提交回复
热议问题