-2
{
  "firstName": "John",
  "lastName": "Smith",
  "gender": "man",
  "age": 32,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ]
}

I want to create directly objects or classes instead of manually to write one by one c# properties like:

        public string firstName{ get; set; }
        public string lastName{ get; set; }
  • Go to jsonutils.com and do it there – Skin Apr 15 '23 at 03:50
  • 1
    Create an empty C# class in Visual Studio, copy your JSON into the clipboard, then use the menu item `Edit > Paste Special > Paste JSON As Classes` - and you get the C# code for your JSON – marc_s Apr 15 '23 at 04:51

1 Answers1

1

There are some handy tools out there that take care of what you're asking. You just need to do some searching online. I've made use of this site in the past, it's quite good:

json2csharp.com

You paste the json snippet into the one panel and convert it and it gives the following output:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Address
{
    public string streetAddress { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postalCode { get; set; }
}

public class PhoneNumber
{
    public string type { get; set; }
    public string number { get; set; }
}

public class Root
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string gender { get; set; }
    public int age { get; set; }
    public Address address { get; set; }
    public List<PhoneNumber> phoneNumbers { get; set; }
}

You might have to make some adjustments but I think it basically does what you're looking for.

SandstormNick
  • 1,821
  • 2
  • 13
  • 24