JSON字符串到Datatable的转换

时间:2019-02-19 09:46:23

标签: c# json datatable

我有以下json字符串。

"data": {
  "message": "Ok",
  "success": true,
  "serverTime": 1550568846,
  "pageNo": 0,
  "pageSize": 100,
  "totalPages": 1,
  "totalCount": 7,
  "list": [
    {
      "vehicleNumber": "NL01N7848",
      "deviceNumber": "358735072950479",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 12.195035555555556,
      "longitude": 76.642826666666664,
      "speed": 0.0,
      "createdDate": 1550497439,
      "location": "Unnamed Road, Byathahalli, Karnataka 571311, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "accurate": false
    },
    {
      "vehicleNumber": "NL01L0067",
      "deviceNumber": "358735073314899",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 26.222473333333333,
      "longitude": 91.702311111111115,
      "speed": 0.0,
      "createdDate": 1550568823,
      "location": "21, Brahmaputra Industrial Park, Gauripur, Amingaon, Guwahati - Baihata Rd, Guwahati, Assam 781030, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "ignition": false,
      "accurate": false
    },
    {
      "vehicleNumber": "NL01L1004",
      "deviceNumber": "358735073306135",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 20.532204444444446,
      "longitude": 85.952977777777775,
      "speed": 0.0,
      "createdDate": 1550568795,
      "location": "Badshahi Road, Alarpur, Odisha 754025, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "ignition": true,
      "accurate": false
    },
    {
      "vehicleNumber": "NL01L2044",
      "deviceNumber": "358735073318866",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 22.567486666666664,
      "longitude": 88.136897777777776,
      "speed": 0.0,
      "createdDate": 1550568787,
      "location": "Amta-Ranihati Road, Ranihati, Mallik Bagan, West Bengal 711302, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "ignition": false,
      "accurate": false
    },
    {
      "vehicleNumber": "NL01L2042",
      "deviceNumber": "358735073311713",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 20.532215555555556,
      "longitude": 85.953057777777772,
      "speed": 0.0,
      "createdDate": 1550568825,
      "location": "Badshahi Road, Alarpur, Odisha 754025, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "ignition": false,
      "accurate": false
    },
    {
      "vehicleNumber": "NL01L6027",
      "deviceNumber": "NA",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 13.265313,
      "longitude": 80.11657,
      "speed": 0.0,
      "createdDate": 1550565467,
      "location": "Tirupati Rd, Manjankaranai, Tamil Nadu 601103, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "accurate": false
    },
    {
      "vehicleNumber": "HR38U3103",
      "deviceNumber": "NA",
      "vendorCode": "WE12881",
      "venndorName": "kapoor Diesels",
      "latitude": 27.594013,
      "longitude": 77.59916,
      "speed": 44.0,
      "createdDate": 1550568763,
      "location": "NH19, Bharthia, Uttar Pradesh 281406, India",
      "provider": "WHEELSEYE",
      "vehicleType": "NA",
      "ignition": true,
      "accurate": false
    }
  ]
}

我想将其转换为C#类对象。 我正在使用以下类结构:

class Data
    {
        JsonData data { get; set; }
    }
    class JsonData
    {
        public string message { get; set; }
        public bool success { get; set; }
        public Int64 serverTime { get; set; }
        public int pageNo { get; set; }
        public int pageSize { get; set; }
        public int totalPages { get; set; }
        public int totalCount { get; set; }
        List<VehicleDetails> list { get; set; }
    }
    class VehicleDetails
    {
        public string vehicleNumber { get; set; }
        public string deviceNumber { get; set; }
        public string vendorCode { get; set; }
        public string venndorName { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public double speed { get; set; }
        public Int64 createdDate { get; set; }
        public string location { get; set; }
        public string provider { get; set; }
        public string vehicleType { get; set; }
        public bool accurate { get; set; }
    }

但是每次我尝试反序列化上述json时,都会出现一个异常,提示传入的无效数组

编辑:

这是反序列化的代码:

string jsonString;
JavaScriptSerializer jSerObj = new JavaScriptSerializer();
List<Data> lstData = (List<Data>) jSerObj.Deserialize(jsonString, typeof(List<Data>));

EDIT-2:

我从json字符串中删除了外部[]

请帮助我。

2 个答案:

答案 0 :(得分:1)

最后我认为这是您的json,

{
  "data": {
    "message": "Ok",
    "success": true,
    "serverTime": 1550568846,
    "pageNo": 0,
    "pageSize": 100,
    "totalPages": 1,
    "totalCount": 7,
    "list": [
      {
        "vehicleNumber": "NL01N7848",
        ...
      },
      {
        "vehicleNumber": "NL01L0067",
        ...
      },
    ]
  }
}

然后,您可以将该json失效,

Data lstData = (Data)jSerObj.Deserialize(jsonString, typeof(Data));

完成上述操作后,您仍然会获得list为空

因此,将访问修饰符public添加到list类的属性JsonDatadata类的Data属性中,因此在应用公共访问修饰符之后

class Data
{
    public JsonData data { get; set; }
}

class JsonData
{
    ...
    public List<VehicleDetails> list { get; set; }
} 

替代:

从json中,如果您想将list密钥数据直接解析为DataTable,则可以使用以下代码

string json = File.ReadAllText(@"Path to your json file");

JToken jToken = JToken.Parse(json);

DataTable dataTable = jToken["data"]["list"].ToObject<DataTable>();

注意:您需要从Nuget软件包管理器安装newtonsoft.json软件包。然后,您需要将using Newtonsoft.Json.Linq;命名空间导入程序

输出:您输出的数据表如下所示:

http://search-microservice.localhost:8002

答案 1 :(得分:0)

您的json字符串无效。它以[]开头和结尾,这表示一个数组。数组元素没有键/值对。只是元素。

任何一个

  1. {}替换开头和结尾。要么;
  2. 一开始就删除data键,只剩下一组JsonData

如果您在上面选择1.,则您的解析代码应变为

Data lstData = (Data) jSerObj.Deserialize(jsonString, typeof(Data));

,如果您选择2,则应为:

List<JsonData> lstData = (List<JsonData>) jSerObj.Deserialize(jsonString, typeof(List<JsonData>));

编辑后:

您的数据仍然无效-现在没有任何数据。您可以使用上面的选项1对其进行解析,方法是在其中拥有的内容后面附加{}


编辑2

您应该使用Json.NET进行反序列化-它远胜过JsonSerializer

代码是:

var result = JsonConvert.DeserializeObject<Data>(jsonString);

实时示例:https://rextester.com/WRXSO2016

(请注意,您在类中还缺少几个public访问修饰符)