JSON没有按预期反序列化?

时间:2017-04-09 04:02:18

标签: c# json json.net

我有一个类似的集合:

public class Xml
{
    public string version { get; set; }
    public string encoding { get; set; }
}

public class Content
{
    public string Expires { get; set; }
    public string MaxArrivalScope { get; set; }
}

public class Trip
{
    public string ETA { get; set; }
    public string TripNo { get; set; }
    public string WheelchairAccess { get; set; }
}

public class Destination
{
    public string Name { get; set; }
    public List<Trip> Trip { get; set; }
}

public class Route
{
    public string RouteNo { get; set; }
    public string Name { get; set; }
    public Destination Destination { get; set; }
}

public class Platform
{
    public string PlatformTag { get; set; }
    public string Name { get; set; }
    public Route Route { get; set; }
}

public class JPRoutePositionET
{
    public string xmlns { get; set; }
    public string xsi { get; set; }
    public string schemaLocation { get; set; }
    public Content Content { get; set; }
    public Platform Platform { get; set; }
}

public class RootObject
{
    public Xml xml { get; set; }
    public JPRoutePositionET JPRoutePositionET { get; set; }
}

}

我有这样的JSON:

{
  "xml": {
    "version": "1.0",
    "encoding": "utf-8"
  },
  "JPRoutePositionET": {
    "xmlns": "urn:connexionz-co-nz:jp",
    "xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "schemaLocation": "urn:connexionz-co-nz:jp JourneyPlanner.xsd",
    "Content": {
      "Expires": "2017-04-09T15:59:31+12:00",
      "MaxArrivalScope": "60"
    },
    "Platform": {
      "PlatformTag": "2628",
      "Name": "Awatea Rd near Awatea Gdns",
      "Route": {
        "RouteNo": "125",
        "Name": "Redwood/Westlake",
        "Destination": {
          "Name": "Westlake & Halswell",
          "Trip": [
            {
              "ETA": "4",
              "TripNo": "4751",
              "WheelchairAccess": "true"
            },
            {
              "ETA": "32",
              "TripNo": "4752",
              "WheelchairAccess": "true"
            }
          ]
        }
      }
    }
  }
}

为什么Newtonsoft无法正确解析Platform类?它返回null。我是否需要格式化JSON以删除我不想要的所有其他信息?还是我想念的东西?

1 个答案:

答案 0 :(得分:0)

法比奥对

的评论

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(js‌​onString);

的工作。

但是现在当我得到像pastebin.com/pebp178这样的JSON时,我似乎无法使用Json2CSharp来获取我需要的类,因为它没有使Destination成为List,它也会改变访问对象而不是类。这真的很奇怪。它只是抛出一个错误,说JSON无法被解析。