反序列化时出错 - 列表和对象

时间:2017-06-22 15:31:26

标签: c# json.net deserialization

我遇到了反序列化的问题。这是返回的错误:

  

无法将当前JSON数组(例如[1,2,3])反序列化为类型“SmartTransportNatif.Stations”,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。 / p>

我从服务器返回了json的这一部分:

"stations": {
        "from": [
            {
                "id": "008501120",
                "name": "Lausanne",
                "score": 101,
                "coordinate": {
                    "type": "WGS84",
                    "x": 46.516777,
                    "y": 6.629095
                },
                "distance": null
            }
        ],
        "to": [
            {
                "id": "000000178",
                "name": "Fribourg",
                "score": null,
                "coordinate": {
                    "type": "WGS84",
                    "x": 46.803272,
                    "y": 7.151027
                },
                "distance": null
            }
        ]
    }

以下是反序列化的对象:

 public class RootObject2
    {
        public List<Connection> connections { get; set; }
        public Station from { get; set; }
        public Station to { get; set; }
        public Stations stations { get; set; }
    }

 public class Stations
    {
        [JsonProperty("from")]
        public List<Station> from { get; set; }

        [JsonProperty("to")]
        public List<Station> to { get; set; }
    }


  public class Station
    {
        public string id { get; set; }
        public string name { get; set; }
        public object score { get; set; }
        public Coordinate coordinate { get; set; }
        public double distance { get; set; }
    }

正如错误提到的那样,导致错误的是“站点”类型,但我看不出有什么问题。

任何人都可以帮助我吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

我假设你在前面和后面都缺少{}。如果是这样,请检查以下内容:

public class Coordinate
{
    public string type { get; set; }
    public double x { get; set; }
    public double y { get; set; }
}

public class From
{
    public string id { get; set; }
    public string name { get; set; }
    public int score { get; set; }
    public Coordinate coordinate { get; set; }
    public object distance { get; set; }
}

public class Coordinate2
{
    public string type { get; set; }
    public double x { get; set; }
    public double y { get; set; }
}

public class To
{
    public string id { get; set; }
    public string name { get; set; }
    public object score { get; set; }
    public Coordinate2 coordinate { get; set; }
    public object distance { get; set; }
}

public class Stations
{
    public List<From> from { get; set; }
    public List<To> to { get; set; }
}

public class RootObject
{
    public Stations stations { get; set; }
}

答案 1 :(得分:0)

让我们首先向后工作,这是你的课程:

  

请仔细查看这些内容,因为我已根据您的json对象为每个属性正确映射了类型。

public class RootObject
{
    public Stations Stations { get; set; }
}

public class Stations
{
    public List<Station> From { get; set; }
    public List<Station> To { get; set; }
}


public class Station
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int? Score { get; set; }
    public Coordinate Coordinate { get; set; }
    public double? Distance { get; set; }
}

public struct Coordinate
{
    public string Type { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
}

以下是您的课程示例,旨在制作您想要的RootObject

var data = new RootObject()
{
    Stations = new Stations()
    {
        From = new List<Station>()
        {
            new Station()
            {
                Id = "008501120",
                Name = "Lausanne",
                Score = 101,
                Coordinate = new Coordinate {Type = "WGS84", X = 46.516777, Y = 6.629095},
                Distance = null
            }
        },
        To = new List<Station>()
        {
            new Station()
            {
                Id = "000000178",
                Name = "Fribourg",
                Score = null,
                Coordinate = new Coordinate {Type = "WGS84", X = 46.803272, Y = 7.151027},
                Distance = null
            }
        }
    }
};

var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);

你得到的价值如下:

{
  "Stations": {
    "From": [
      {
        "Id": "008501120",
        "Name": "Lausanne",
        "Score": 101,
        "Coordinate": {
          "Type": "WGS84",
          "X": 46.516777,
          "Y": 6.629095
        },
        "Distance": null
      }
    ],
    "To": [
      {
        "Id": "000000178",
        "Name": "Fribourg",
        "Score": null,
        "Coordinate": {
          "Type": "WGS84",
          "X": 46.803272,
          "Y": 7.151027
        },
        "Distance": null
      }
    ]
  }
}

反之亦然。

var foo = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);

证明它有效

http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg
根据您上面的链接,以下是行动中的课程

var httpWebRequest = (HttpWebRequest)WebRequest
    .Create("http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadLine();
    var foo = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(result);
}

工作正常。

您的错误消息

您收到的错误:

  

无法将当前JSON数组(例如[1,2,3])反序列化为类型'SmartTransportNatif.Stations',因为该类型需要JSON对象(例如{“name”:“value” })正确地反序列化。

这指向一个你甚至没有在你的问题中显示的类,它只是错误地建模到json。但由于没有提供您的问题,上面的答案显示足以显示您出错的地方。