0

I am having the following json string.

"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
    }
  ]
}

I want to convert it to a C# class object. I am using the following class structure:

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; }
    }

But every time when I am trying to deserialize the above json, an exception is coming that says Invalid array passed in

EDIT:

Here is the code of deserialization:

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

EDIT-2:

I have removed the outer [] from my json string

Please help me on this.

14
  • 1
    Where is your deserialization code? Commented Feb 19, 2019 at 9:48
  • your example JSON string in a JSON-Array and not an JSON-object as it starts with [ and ends with ]. So, if your code uses something like JsonConvert.DeserializeObject<Data>(<your json string here>); change it to something like JsonConvert.DeserializeObject<List<Data>>(<your json string here>);. Commented Feb 19, 2019 at 9:49
  • 2
    Your Json is not in valid format. It would be better you begin by examining and correcting your json Commented Feb 19, 2019 at 9:55
  • Upload it to jsonformatter.curiousconcept.com and you will get an error Error:Expecting comma or ], not colon., or jsonlint.com which reports Error: Parse error on line 1: Expecting 'EOF', '}', ',', ']', got ':' The basic problem is that the outer container delimiter is [ indicating an array, but "data": {} is an object property. Commented Feb 19, 2019 at 10:00
  • @er-sho I have added the code that I am using for deserialization Commented Feb 19, 2019 at 10:01

2 Answers 2

1

Finally I considered as this is your json,

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

Then you can deserailize this json as,

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

After doing above stuff you still get list as null

So add access modifier public to property list in your JsonData class and data property in Data class, So after applying public access modifier

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

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

Alternative:

From your json, If you want to parse your list key data directly to DataTable then you can use below code

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

JToken jToken = JToken.Parse(json);

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

Note: You need to install newtonsoft.json package from Nuget Package Manager. And then you need to import using Newtonsoft.Json.Linq; namespace to your program

Output: You output data table look like

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

worked like a charm... I used your alternative solution
Yes I think so, you want parse list to direct datatable that's why I'd provide you an alternative, glad to hear and welcome :)
0

Your json string is invalid. It starts and ends with [ & ] which implies an array. Array elements do not have key/value pairs. Just elements.

Either

  1. Replace the start and end with { and }. or;
  2. Remove the data key at the start and just have an array of JsonData

If you choose 1. above, your parsing code should become

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

and if you chose 2. it should be:

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

After your edit:

Your data is still not valid - now it has nothing around it. You can parse it using option 1. above by appending { and } around what you have there.


Edit 2

You should use Json.NET for deserialization - it is far superior to JsonSerializer

Code is:

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

Live example: https://rextester.com/WRXSO2016

(Note you were also missing a couple of public access modifiers in your classes)

4 Comments

I tried the 1st solution as given by you.. it is giving data as null
I tried exactly what is given in 1st solution... still the data is null
@DeepakVerma see Edit 2.
it worked ... thanks a lot mate... however I applied the solution as given above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.