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

时光怂恿深爱的人放手 提交于 2020-12-27 07:22:28

问题


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

JSON

{  
   "warnings":[  
      {  
         "message":"Number is in DND",
         "numbers":"917000000000"
      }
   ],
   "balance":900,
   "batch_id":311110011,
   "cost":1,
   "num_messages":1,
   "message":{  
      "num_parts":1,
      "sender":"TXTLCL",
      "content":"Test1"
   },
   "receipt_url":"",
   "custom":"",
   "inDND":[  
      "917000000000"
   ],
   "messages":[  
      {  
         "id":"1350123781",
         "recipient":918819437284
      }
   ],
   "status":"success"
}

My code with which I'm trying to parse the JSON:

private void button1_Click(object sender, EventArgs e)
{
    var a = JsonConvert.DeserializeObject<List<jsonToObj[]>>(richTextBox1.Text);
}

public class jsonToObj
{
    public warnings[] 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 messages[] messages { get; set; }
    public string status { get; set; }
}


public class warnings
{
    public string message { get; set; }
    public string numbers { get; set; }
}

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

public class message
{
    public int num_part { get; set; }
    public string sender { get; set; }
    public string content { get; set; }
}

I'm getting an exception with the following message:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WindowsFormsApp1.Form2+jsonToObj[]]' 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. Path 'warnings', line 1, position 12.'


回答1:


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; }
  ...
}




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/49680094/converting-json-to-object-fails-cannot-deserialize-the-current-json-object-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!