从json字符串反序列化对象没有获取值

时间:2017-03-25 10:12:20

标签: c# json deserialization

我有一个json文件我希望Json反序列化所有对象,但它在我的程序中反序列化时返回注意

Json字符串

 public class CurrentRoutes
{
    public class TrackingData
    {
        public string driverId { get; set; }
        public string vehicleId { get; set; }
        public int timeInSec { get; set; }
        public List<int> timeInLatLng { get; set; }
        public int timeOutSec { get; set; }
        public List<int> timeOutLatLng { get; set; }
        public string status { get; set; }
        public int statusSec { get; set; }
        public List<int> statusLatLng { get; set; }
        public int timeInDetectedSec { get; set; }
        public object timeInDetectedLatLng { get; set; }
        public int timeOutDetectedSec { get; set; }
        public object timeOutDetectedLatLng { get; set; }
        public object pods { get; set; }
    }

    public class Step
    {
        public string type { get; set; }
        public int idleTimeSec { get; set; }
        public int perStopTimeSec { get; set; }
        public int arrivalSec { get; set; }
        public int startSec { get; set; }
        public int endSec { get; set; }
        public int driveToNextSec { get; set; }
        public int distanceToNextMt { get; set; }
        public string displayLabel { get; set; }
        public string orderId { get; set; }
        public TrackingData trackingData { get; set; }
    }

    public class Orders
    {
    }

    public class Vehicles
    {
    }

    public class Depots
    {
    }

    public class Drivers
    {
    }

    public class RootObject
    {
        public Routes routes { get; set; }
        public Orders orders { get; set; }
        public Vehicles vehicles { get; set; }
        public Depots depots { get; set; }
        public Drivers drivers { get; set; }
    }

    public class Routes
    {
        public string id { get; set; }
        public int revision { get; set; }
        public string date { get; set; }
        public string vehicleId { get; set; }
        public string driverId { get; set; }
        public List<Step> steps { get; set; }
    }



}

我为脱盐

创建了这个类
CurrentRoutes.RootObject rootObj = JsonConvert.DeserializeObject<CurrentRoutes.RootObject>(pikdelresponse);

我的代码在c#下面的代码我在pikdelresponse中传递json

click

你能告诉我如何获得上面json的价值。感谢您的评论和代码。

2 个答案:

答案 0 :(得分:1)

一种选择是使用Dictionary<string, Route>。例如:

public class RootObject
{
    public Dictionary<string, Route> Routes { get; set; }
    public Orders orders { get; set; }
    public Vehicles vehicles { get; set; }
    public Depots depots { get; set; }
    public Drivers drivers { get; set; }
}

public class TrackingData
{
    public string driverId { get; set; }
    public string vehicleId { get; set; }
    public int timeInSec { get; set; }
    public List<int> timeInLatLng { get; set; }
    public int timeOutSec { get; set; }
    public List<int> timeOutLatLng { get; set; }
    public string status { get; set; }
    public int statusSec { get; set; }
    public List<int> statusLatLng { get; set; }
    public int timeInDetectedSec { get; set; }
    public object timeInDetectedLatLng { get; set; }
    public int timeOutDetectedSec { get; set; }
    public object timeOutDetectedLatLng { get; set; }
    public object pods { get; set; }
}

public class Step
{
    public string type { get; set; }
    public int idleTimeSec { get; set; }
    public int perStopTimeSec { get; set; }
    public int arrivalSec { get; set; }
    public int startSec { get; set; }
    public int endSec { get; set; }
    public int driveToNextSec { get; set; }
    public int distanceToNextMt { get; set; }
    public string displayLabel { get; set; }
    public string orderId { get; set; }
    public TrackingData trackingData { get; set; }
}

public class Route
{
    public string id { get; set; }
    public int revision { get; set; }
    public string date { get; set; }
    public string vehicleId { get; set; }
    public string driverId { get; set; }
    public List<Step> steps { get; set; }
}

并像这样反序列化:

var rootObj = JsonConvert.DeserializeObject<RootObject>(pikdelresponse);

答案 1 :(得分:0)

删除

TabC.x = "null"

你的json应该是这样的

"3d3d6f6c-5d05-4a4e-a17e-1b128df78c20-20170324": {

}

它会没问题,或像这样更改你的类/对象树声明

{


 "routes": {
      "id": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20-20170324",
      "revision": 457,
      "date": "20170324",
      "vehicleId": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20",
      "driverId": "f8ddf3fc-8e70-4edd-b0b4-5e41cf86d72a",
      "steps": [
        {
          "type": "departure",
          "idleTimeSec": 0,
          "perStopTimeSec": 0,
          "arrivalSec": 0,
          "startSec": 0,
          "endSec": 7200,
          "driveToNextSec": -1,
          "distanceToNextMt": -1,
          "displayLabel": ""
        },
        {
          "type": "pickup",
          "orderId": "cd0711f2-f43e-48e9-97db-d76822fbcd54",
          "idleTimeSec": 0,
          "perStopTimeSec": 0,
          "arrivalSec": 8460,
          "startSec": 8460,
          "endSec": 9000,
          "driveToNextSec": -1,
          "distanceToNextMt": -1,
          "displayLabel": "1.1",
          "trackingData": {
            "driverId": "f8ddf3fc-8e70-4edd-b0b4-5e41cf86d72a",
            "vehicleId": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20",
            "timeInSec": 8460,
            "timeInLatLng": [
              28550470,
              -81211578
            ],
            "timeOutSec": 9000,
            "timeOutLatLng": [
              28550470,
              -81211578
            ],
            "status": "done",
            "statusSec": 8761,
            "statusLatLng": [
              28550470,
              -81211578
            ],
            "timeInDetectedSec": -1,
            "timeInDetectedLatLng": null,
            "timeOutDetectedSec": -1,
            "timeOutDetectedLatLng": null,
            "pods": null
          }
        },
        {
          "type": "delivery",
          "orderId": "cd0711f2-f43e-48e9-97db-d76822fbcd54",
          "idleTimeSec": 0,
          "perStopTimeSec": 0,
          "arrivalSec": 9060,
          "startSec": 9060,
          "endSec": 9120,
          "driveToNextSec": -1,
          "distanceToNextMt": -1,
          "displayLabel": "1.2",
          "trackingData": {
            "driverId": "f8ddf3fc-8e70-4edd-b0b4-5e41cf86d72a",
            "vehicleId": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20",
            "timeInSec": 9060,
            "timeInLatLng": [
              28472604,
              -81398655
            ],
            "timeOutSec": 9120,
            "timeOutLatLng": [
              28472604,
              -81398655
            ],
            "status": "done",
            "statusSec": 9314,
            "statusLatLng": [
              28472604,
              -81398655
            ],
            "timeInDetectedSec": -1,
            "timeInDetectedLatLng": null,
            "timeOutDetectedSec": -1,
            "timeOutDetectedLatLng": null,
            "pods": null
          }
        }
      ]
  },
  "orders": {},
  "vehicles": {},
  "depots": {},
  "drivers": {}
}