Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON字符串反序列化为C#对象_C#_Json_Deserialization - Fatal编程技术网

将JSON字符串反序列化为C#对象

将JSON字符串反序列化为C#对象,c#,json,deserialization,C#,Json,Deserialization,我有以下json字符串 [ { "data": { "vehicle_number": "HR38Y1539", "device_id": "0358735074626234", "type": "we_track", "cordinate": [ "28.427771", "77.018409" ], "address": "Haryana",

我有以下json字符串

[
{
    "data": {
        "vehicle_number": "HR38Y1539",
        "device_id": "0358735074626234",
        "type": "we_track",
        "cordinate": [
            "28.427771",
            "77.018409"
        ],
        "address": "Haryana",
        "city": "",
        "state": "",
        "motion_status": "halt",
        "motion_state": "stopped",
        "speed": 0,
        "orientation": "306",
        "last_received_at": 1555407765,
        "share_link": "https://loconav.com/track-a-day/541387a344",
        "custom_data": {}
    }
},
{
    "data": {
        "vehicle_number": "UP63AE7715",
        "device_id": "0866551031969632",
        "type": "gt06mini",
        "cordinate": [
            "28.757673",
            "77.160532"
        ],
        "address": "Ganpati Sachidanand Datta Marg,Swaroop Nagar,Bhalswa,Delhi,North West Delhi",
        "city": "",
        "state": "",
        "motion_status": "halt",
        "motion_state": "moving",
        "speed": 0,
        "orientation": "243",
        "last_received_at": 1555407768,
        "share_link": "https://loconav.com/track-a-day/54961f9617",
        "custom_data": {}
    }
}]
我想将其反序列化为定制的c#对象或数据表。我尝试了各种各样的类定义,但没有一个成功。 在此方面的任何帮助都将不胜感激

编辑:

我为这个json创建了以下类:

public class CustomData
{
}

public class VehicleData
{
    public string vehicle_number { get; set; }
    public string device_id { get; set; }
    public string type { get; set; }
    public List<string> cordinate { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string motion_status { get; set; }
    public string motion_state { get; set; }
    public int speed { get; set; }
    public string orientation { get; set; }
    public int last_received_at { get; set; }
    public string share_link { get; set; }
    public CustomData custom_data { get; set; }
}

public class AUL_Json_Data
{
    public VehicleData data { get; set; }
}
公共类自定义数据
{
}
公共级车辆数据
{
公共字符串车辆号{get;set;}
公共字符串设备\u id{get;set;}
公共字符串类型{get;set;}
公共列表cordinate{get;set;}
公共字符串地址{get;set;}
公共字符串city{get;set;}
公共字符串状态{get;set;}
公共字符串运动状态{get;set;}
公共字符串运动状态{get;set;}
公共整数速度{get;set;}
公共字符串方向{get;set;}
上次在{get;set;}收到的公共int
公共字符串共享链接{get;set;}
公共自定义数据自定义_数据{get;set;}
}
公共类AUL_Json_数据
{
公共车辆数据{get;set;}
}
我正在使用以下代码进行反序列化:

AUL_Json_Data truckData = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<AUL_Json_Data>(json);
AUL_Json_Data truckData=new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(Json);

但它给出了一个例外,即:数组的反序列化不支持类型“AUL_Json_Data”。

对Json使用此模型:

    public class Data
    {
        public string vehicle_number { get; set; }
        public string device_id { get; set; }
        public string type { get; set; }
        public List<string> cordinate { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string motion_status { get; set; }
        public string motion_state { get; set; }
        public int speed { get; set; }
        public string orientation { get; set; }
        public int last_received_at { get; set; }
        public string share_link { get; set; }
        public CustomData custom_data { get; set; }
    }

    public class RootObject
    {
        public Data data { get; set; }
    }
    public class CustomData
    {
    }
公共类数据
{
公共字符串车辆号{get;set;}
公共字符串设备\u id{get;set;}
公共字符串类型{get;set;}
公共列表cordinate{get;set;}
公共字符串地址{get;set;}
公共字符串city{get;set;}
公共字符串状态{get;set;}
公共字符串运动状态{get;set;}
公共字符串运动状态{get;set;}
公共整数速度{get;set;}
公共字符串方向{get;set;}
上次在{get;set;}收到的公共int
公共字符串共享链接{get;set;}
公共自定义数据自定义_数据{get;set;}
}
公共类根对象
{
公共数据数据{get;set;}
}
公共类自定义数据
{
}
然后以这种方式反序列化它

    var Data = JsonConvert.DeserializeObject<List<RootObject>>(data);
var Data=JsonConvert.DeserializeObject(数据);

您可以使用外部在线工具轻松生成基于json的C#模型。搜索json到c#,然后选择一个。你可以将生成的模型与你的类进行比较,看看哪里出了问题。关于这一点,有很多资料,只需在研究中投入一些精力。你可以在Visual Studio中直接轻松地生成C#对象/模型,请参见我的答案。