当返回的值可以是数组或单个项目时,无法反序列化当前JSON数组 [英] Cannot deserialize the current JSON array when returned value can be either array or single item

查看:191
本文介绍了当返回的值可以是数组或单个项目时,无法反序列化当前JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Newtonsoft.Json的新手,所以请原谅我的无知-但是,在尝试将以下Json反序列化为c#对象或手动反序列化时,我遇到了这个问题.

I am new to Newtonsoft.Json so please excuse my ignorance - however I am coming up against this issue when trying to Deserialize the following Json to either a c# object or indeed manually.

Json是

{
  "travellerInfo": [
    {
      "passengerData": {
        "travellerInformation": {
          "passenger": [
            {
              "type": "ADT",
              "firstName": "MARY MRS"
            },
            {
              "type": "INF",
              "firstName": "JOSHUA"
            }
          ],
          "traveller": {
            "surname": "SMITH",
            "quantity": "2"
          }
        }
      }
    },
    {
      "passengerData": {
        "travellerInformation": {
          "passenger": {
            "type": "ADT",
            "firstName": "JOHN MR"
          },
          "traveller": {
            "surname": "SMITH",
            "quantity": "1"
          }
        }
      }
    }
  ]
}

因此,您可以看到,在第一个"passenger"项上,它以数组形式返回,但是在第二个"passenger"项上,它不是以数组形式返回,而只是一个块.我无法控制发送给我的Json,它来自外部系统.我的C#类是

So as you can see, on the first 'passenger' item, this returns as an Array, however on the second 'passenger' item, it doesn't return as an array, just a single block. I am not in control of the Json being sent to me - it comes from an external system. My C# classes are

public class Passenger
{
    public string type { get; set; }
    public string firstName { get; set; }
}

public class Traveller
{
    public string surname { get; set; }
    public string quantity { get; set; }
}

public class TravellerInformation
{
    public List<Passenger> passenger { get; set; }
    public Traveller traveller { get; set; }
}

public class PassengerData
{
    public TravellerInformation travellerInformation { get; set; }
}

public class TravellerInfo
{
   public PassengerData passengerData { get; set; }
}

我打电话

var example = JsonConvert.DeserializeObject<TravellerInfo>(jsonString);

我遇到了错误

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Script1.TravellerInfo' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'travellerInfo', line 57, position 20. 

我尝试在[乘客]类上放置[JsonArray]属性,以强制将其反序列化为数组/列表,但是发生了同样的错误,因为我认为基础项是JProperty而不是JObject.

I tried putting a [JsonArray] attribute on the Passenger class to force it to deserialise as an array/list, but same error occured as I think the underlying item is a JProperty instead of a JObject.

那么当["passenger"]既可以作为数组对象又可以作为单个对象返回时,如何使它起作用?

So how can I get this to work when the ["passenger"] can come back as both an Array and Single object ?

预先加油

推荐答案

尝试一下.将List<Passenger>替换为TravellerInformation中的object passenger:

Try this. Replace List<Passenger> with object passenger in TravellerInformation:

public class Traveller
{
    public string surname { get; set; }
    public string quantity { get; set; }
}

public class TravellerInformation
{
    public object passenger { get; set; }
    public Traveller traveller { get; set; }
}

public class PassengerData
{
    public TravellerInformation travellerInformation { get; set; }
}

public class TravellerInfo
{
    public PassengerData passengerData { get; set; }
}

并通过传递List<TravellerInfo>而不是TravellerInfo来调用它:

And call this by passing List<TravellerInfo> instead of TravellerInfo:

var example = JsonConvert.DeserializeObject<List<TravellerInfo>>(jsonString);

对于这些情况,您还可以使用服务,该服务会自动从JSON对象创建C#类,因此您不必不必担心正确性.

Also for these cases you can use this service which automatically creates C# classes from JSON objects, so you don't have to worry about correctness.

这篇关于当返回的值可以是数组或单个项目时,无法反序列化当前JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆