Serialization using Json.NET

suggest change
public class Person
    public string PersonName { get; set; }
    public int PersonAge { get; set; }
    public string Address { get; set; }

Person person = new Person { PersonName = "Andrius", PersonAge = 99, Address = "Some address" };
string rawJson = JsonConvert.SerializeObject(person);

Console.WriteLine(rawJson); // {"name":"Andrius","age":99}

Notice how properties (and classes) can be decorated with attributes to change their appearance in resulting json string or to remove them from json string at all (JsonIgnore).

More information about Json.NET serialization attributes can be found here.

In C#, public identifiers are written in PascalCase by convention. In JSON, the convention is to use camelCase for all names. You can use a contract resolver to convert between the two.

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public class Person
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }

public void ToJson() {
    Person person = new Person { Name = "Andrius", Age = 99, Address = "Some address" };
    var resolver = new CamelCasePropertyNamesContractResolver();
    var settings = new JsonSerializerSettings { ContractResolver = resolver };
    string json = JsonConvert.SerializeObject(person, settings);

    Console.WriteLine(json); // {"name":"Andrius","age":99}

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents