当返回值可以是数组或单个项时,无法反序列化当前JSON数组

时间:2017-02-14 15:27:49

标签: c# arrays json json.net

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

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"
          }
        }
      }
    }
  ]
}

正如您所看到的,在第一个“乘客”项目上,它返回一个数组,但是在第二个“乘客”项目中,它不会作为数组返回,只返回一个数据块。我无法控制Json被发送给我 - 它来自外部系统。我的C#类是

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. 

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

那么当[“passenger”]可以作为Array和Single对象返回时,如何才能使它工作?

提前干杯

2 个答案:

答案 0 :(得分:0)

试试这个。将List<Passenger>替换为object passenger中的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来调用此方法:

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

同样对于这些情况,您可以使用此service自动从JSON对象创建C#类,因此您不必担心正确性。

答案 1 :(得分:0)

您可以尝试将其反序列化为dynamic,然后对类型进行一些检查。检查它是否为is IEnumerable

那应该做的。