[JsonObject("person")]
public class Person
{
[JsonProperty("name")]
public string PersonName { get; set; }
[JsonProperty("age")]
public int PersonAge { get; set; }
[JsonIgnore]
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; }
[JsonIgnore]
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}
}