SoFunction
Updated on 2025-03-06

Summary of common methods in c#

1 Json serialization and deserialization of entity classes

Let's take the following Person class as an example, which contains commonly used data types:

public class Person
{
 public int ID { get; set; }

 public string Name { get; set; }

 public DateTime Birthday { get; set; }

 public bool IsVIP { get; set; }
 
 public float Account { get; set; }

 public string[] Favorites { get; set; }
 
 public string Remark { get; set; }
}

Create aPersonExample:

Person person = new Person
{
 ID = 1,
 Name = "Zhang San",
 Birthday = ("2000-01-02"),
 IsVIP = true,
 Account = 12.34f,
 Favorites = new string[] { "Have a meal", "sleep" }
};

1.1 Json Serialization

Returns an unindented Json string

(person);

{"ID":1,"Name":"Zhang San","Birthday":"2000-01-02T00:00:00","IsVIP":true,"Account":12.34,"Favorites":["Have a meal","sleep"],"Remark":null}

Returns the indented Json string

(person, );

{
 "ID": 1,
 "Name": "Zhang San",
 "Birthday": "2000-01-02T00:00:00",
 "IsVIP": true,
 "Account": 12.34,
 "Favorites": [
  "Have a meal",
  "sleep"
 ],
 "Remark": null
}

1.2 Convert unindented JSON strings to indented form

private string JsonIndentation(string str)
{
 //string str = (entity);
 JsonSerializer serializer = new JsonSerializer();
 TextReader tr = new StringReader(str);
 JsonTextReader jtr = new JsonTextReader(tr);
 object obj = (jtr);
 if (obj != null)
 {
  StringWriter textWriter = new StringWriter();
  JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
  {
   Formatting = ,
   Indentation = 4,
   IndentChar = ' '
  };
  (jsonWriter, obj);
  return ();
 }
 else
 {
  return str;
 }
}

or:

private string JsonIndentation(string json)
{
 JObject obj = (json);
 return ();
}

1.3 Other settings

JsonSerializerSettings settings = new JsonSerializerSettings();
// Set date format = "yyyy-MM-dd";
// Ignore empty values = ;
// Indent = ;

(person, settings);

return:

{
 "ID": 1,
 "Name": "Zhang San",
 "Birthday": "2000-01-02",
 "IsVIP": true,
 "Account": 12.34,
 "Favorites": [
 "Have a meal",
 "sleep"
 ]
}

1.4 Json Deserialization

<Person>(json);

2 JObject Use

2.1 Creating an object

JObject obj = new JObject();
("ID", 1);
("Name", "Zhang San");
("Birthday", ("2000-01-02"));
("IsVIP", true);
("Account", 12.34f);
// Create an arrayJArray array = new JArray();
(new JValue("Have a meal"));
(new JValue("sleep"));
("Favorites", array);
("Remark", null);

2.2 Adding an array in JObject

The code in the above example can be simplified to:

JArray array = new JArray("Have a meal", "sleep");

2.3 Creating a JObject from a Json string

string json = "{\"ID\":1,\"Name\":\"Zhang San\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"Have a meal\",\"sleep\"],\"Remark\":null}";

JObject obj = (json);

2.4 Create a JObject from Entity

JObject obj = (person);

Create a JObject with anonymous object

JObject obj = (new { name = "jack", age = 18 });

//show{
 "name": "jack",
 "age": 18
}

Use the initializer

JObject obj = new JObject()
{
 { "name", "jack" },
 { "age", 18 }
};

2.5 Get the value

int id;
if (obj["ID"] != null)
 id = obj["ID"].Value<int>();

2.6 Get array

It is not supported to get arrays directly, but you can get List and then convert them into arrays.

string[] favorites;
if (obj["Favorites"] != null)
 favorites = obj["Favorites"].Value<List<string>>().ToArray();

The above is the detailed content of the commonly used methods of c#. For more information about c#, please pay attention to my other related articles!