Putting JSON reponse into C# Class RRS feed

  • Question

  • Please help, I am at a stand still.

    I cannot figure out how to get this response format into an object.

    I would like it to ultimately go into a Company.cs class.

    I am trying to us Newton.Json.


    { "items": [ { "id": 29858517, "companyName": " Union Second Market Bank", "createDate": "2017-05-27T19:04:30.307", "isActive": true, "userDefinedFields": [ { "name": "CustomerRanking", "value": "Golden" }, { "name": "CustomerSize", "value": null } ] }, { "id": 30070091, "companyName": "100% Recycled Paperboard Industries", "createDate": "2018-03-22T16:00:52.78", "isActive": true, "userDefinedFields": [ { "name": "CustomerRanking", "value": "Not Golden" }, { "name": "CustomerSize", "value": 50 } ] } ], "pageDetails": { "count": 2, "requestCount": 500, "prevPageUrl": null, "nextPageUrl": null } }

    Jason Munsterteiger

    Tuesday, July 21, 2020 3:30 PM

All replies

  • Create a new class file. Then copy the JSON and paste it using Edit/Paste Special. This will create the necessary class(es).

    Tuesday, July 21, 2020 3:38 PM
  • Copy the JSON, in Visual Studio select Edit menu, Paste special, Paste JSON as classes. You can do this in any class in your project to generate the classes then move then to separate files.

    This is what is generated when I did it.

        public class Company
            public Item[] items { get; set; }
            public Pagedetails pageDetails { get; set; }
        public class Pagedetails
            public int count { get; set; }
            public int requestCount { get; set; }
            public object prevPageUrl { get; set; }
            public object nextPageUrl { get; set; }
        public class Item
            public int id { get; set; }
            public string companyName { get; set; }
            public DateTime createDate { get; set; }
            public bool isActive { get; set; }
            public Userdefinedfield[] userDefinedFields { get; set; }
        public class Userdefinedfield
            public string name { get; set; }
            public object value { get; set; }

    To test, place the json into a file and code would be like the following, if there is more than one record DeserializeObject would be passed List<Company>

    var json = File.ReadAllText(
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sample.json"));
    var results = JsonConvert.DeserializeObject<Company>(json);

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Tuesday, July 21, 2020 3:43 PM
  • Hi

    Take reference of Newtonsoft for Jsonconvertor 

    PM> Install-Package Newtonsoft.Json

    using System.Web.Script.Serialization;

    JavaScriptSerializer js = new JavaScriptSerializer();  
    var Companys = js.Deserialize<Company>(jsonData);  

    Thanks and regards

    Tuesday, July 21, 2020 4:08 PM