-3

I have the following json string

[
{
    "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": {}
    }
}]

I want to deserialize it to either into a customized c# object or datatable. I tried making various class definitions but none of them succeeded. Any help on this would be greatly appreciated.

EDIT:

I made the following class for this 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; }
}

and I am using the following code to deserialize:

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

But it is giving an exception that says : Type 'AUL_Json_Data' is not supported for deserialization of an array.

DeepakVerma
  • 99
  • 10
  • 1
    You can easily generate C# models based on json using external online tools. Search for json to c# and pick one. You can compare generated models with your classes and see what was wrong. – Jacek Apr 16 '19 at 10:08
  • 1
    There are tons of material about that, just put some effort into research. – Sá´‡M Apr 16 '19 at 10:09
  • You can easily generate the C# object/model directly in Visual Studio, see my answer [here](https://stackoverflow.com/a/30561475/4302070). – trashr0x Apr 16 '19 at 10:21

1 Answers1

0

use this model for your 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
    {
    }

And then deserialize it this way

    var Data = JsonConvert.DeserializeObject<List<RootObject>>(data);
Hitesh Anshani
  • 1,499
  • 9
  • 19