'无法将当前JSON对象(例如{" Name":" value"})反序列化为类型'system.collections.generic.list`1 ... [英] 'Cannot deserialize the current JSON object (e.g. {"Name":"value"}) into type 'system.collections.generic.list`1...

查看:141
本文介绍了'无法将当前JSON对象(例如{" Name":" value"})反序列化为类型'system.collections.generic.list`1 ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我得到了这样的JSON,并希望将其反序列化为我的C#对象。



JSON(部分):



Hello, I get such JSON and want to deserialize it into my C# object.

JSON (partially):

{
    "Quotes": [
        {
            "QuoteId": 1,
            "MinPrice": 119,
            "Direct": true,
            "OutboundLeg": {
                "CarrierIds": [
                    851
                ],
                "OriginId": 81727,
                "DestinationId": 60987,
                "DepartureDate": "2019-04-01T00:00:00"
            },
            "QuoteDateTime": "2019-03-10T19:27:00"
        }
    ],
    "Places": [
        {
            "PlaceId": 60987,
            "IataCode": "JFK",
            "Name": "New York John F. Kennedy",
            "Type": "Station",
            "SkyscannerCode": "JFK",
            "CityName": "New York",
            "CityId": "NYCA",
            "CountryName": "United States"
        },
        {
            "PlaceId": 81727,
            "IataCode": "SFO",
            "Name": "San Francisco International",
            "Type": "Station",
            "SkyscannerCode": "SFO",
            "CityName": "San Francisco",
            "CityId": "SFOA",
            "CountryName": "United States"
        }
    ]   
}





我的课程:



My classes:

public class MyQuotes

    {
        public List<Quote> Quotes { get; set; }
        public List<Place> Places { get; set; }
    }

    public class Quote
    {
        public int QuoteId { get; set; }
        public List<MyOutboundLeg> OutboundLeg { get; set; }
    }

    public class MyOutboundLeg
    {
        public List<int> CarrierIds { get; set; }
        public int OriginId { get; set; }
    }

    public class Place
    { 
        public string PlaceId { get; set; }
        public string CountryName { get; set; }
    }
    
    }





我的尝试:





What I have tried:

var body = ApiTestContext.Response.Content.ReadAsStringAsync().Result;
            MyQuotes myQuotes = JsonConvert.DeserializeObject<MyQuotes>(body);

推荐答案

不要把所有字段都放在json必须出现在类中,并且它们是否都必须在JSON和类中命名相同?怎么还能序列化?



你在类定义中缺少字段,其中一个字段的名称与其名称相同在json数据中(这是异常发生的地方--JSON有Name,你的类有PlaceName)。
Don't all of the fields in the json have to be present in the class, AND don't they all have to be named the same in the JSON and the class? How else is it going to be able to serialize?

You're missing fields in your class definition, and one of the fields isn't named the same as what's in the json data (this is where the exception is happening - JSON has "Name", and your class has "PlaceName").


那是因为你的JSON与你的类定​​义不匹配。

尝试在线生成器(这是我使用的一个: json2csharp - 从json生成c#类 [ ^ ])你得到这个:

That's because your JSON doesn't match your class definitions.
Try an online generator (here's the one I use: json2csharp - generate c# classes from json[^] ) and you get this:
public class OutboundLeg
{
    public List<int> CarrierIds { get; set; }
    public int OriginId { get; set; }
    public int DestinationId { get; set; }
    public DateTime DepartureDate { get; set; }
}

public class Quote
{
    public int QuoteId { get; set; }
    public int MinPrice { get; set; }
    public bool Direct { get; set; }
    public OutboundLeg OutboundLeg { get; set; }
    public DateTime QuoteDateTime { get; set; }
}

public class Place
{
    public int PlaceId { get; set; }
    public string IataCode { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string SkyscannerCode { get; set; }
    public string CityName { get; set; }
    public string CityId { get; set; }
    public string CountryName { get; set; }
}

public class RootObject
{
    public List<Quote> Quotes { get; set; }
    public List<Place> Places { get; set; }
}


您的Outbound Leg类是JSON中的属性,但是Quote类中的List:



Your Outbound Leg class is a property in your JSON but a List in your Quote class:

public class Quote
    {
        public int QuoteId { get; set; }
        public List<MyOutboundLeg> OutboundLeg { get; set; }
    }





应该是:





Should be:

public class Quote
    {		
        public int QuoteId { get; set; }
        public MyOutboundLeg OutboundLeg { get; set; }
    }


这篇关于'无法将当前JSON对象(例如{&quot; Name&quot;:&quot; value&quot;})反序列化为类型'system.collections.generic.list`1 ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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