SoFunction
Updated on 2025-03-08

In C#, JSON is converted to entity class and List and combined

Quote

using ;
using ;

JSON to Entity Class

public class Person
{
	public string Name { get; set; }
	public int Age { get; set; }
	public string Gender { get; set; }
}
string jsonStr = "{\"name\": \"Tom\", \"age\": 20, \"gender\": \"male\"}";
Person person = <Person>(jsonStr);

JSON to List

Method 1

json

{
   "data": [
         {
            "Id":"0",
            "Username":"Vreeswijk",
            "ProfilePicture":"media/user/"
         },
         {
            "Id":"1",
            "Username":"Mony",
            "ProfilePicture":"media/user/"
         }
      ]
}

accomplish

JObject obj = (json);

//Shou is the class corresponding to an element in the array in jsonList&lt;Shou&gt; root = &lt;List&lt;Shou&gt;&gt;(obj["data"].ToString());

Method 2

json

{
   "user":{
      [
         {
            "Id":"0",
            "Username":"Vreeswijk",
            "ProfilePicture":"media/user/"
         }
      ]
   },
   "token":{
      [
         {
            "access_token":"myToken1",
            "refresh_token":"myToken2",
            "expires_in":3600,
            "expires_on":1577363756
         }
      ]
   }
}

Two categories

public class Token
{
    public string access_token { get; set; }
    public string refresh_token { get; set; }
    public int expire_in { get; set; }
    public int expire_on { get; set; }
	//The construction method is omitted}

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string ProfilePicture { get; set; }
	//The construction method is omitted}

accomplish

var jObject = (json);
var userPropery = jObject["user"] as JArray;
List&lt;User&gt; userList= new List&lt;User&gt;();

foreach (var property in userPropery )
{
    var propertyList = &lt;List&lt;user&gt;&gt;(());
	//The difference between the AddRange method in the List collection is that the Add method is to add a single element object at the end of the collection, while the AddRange method can add the entire target list collection.    (propertyList);
}

Use in combination

json

{
   "number": 1,
   "day": "September",
   "user":{
      [
         {
            "Id":"0",
            "Username":"Vreeswijk",
            "ProfilePicture":"media/user/"
         }
      ]
   },
   "token":{
       "access_token":"myToken1",
       "refresh_token":"myToken2",
       "expires_in":3600,
       "expires_on":1577363756
   }
}

Two categories

public class Token
{
    public string access_token { get; set; }
    public string refresh_token { get; set; }
    public int expire_in { get; set; }
    public int expire_on { get; set; }
	//The construction method is omitted}

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string ProfilePicture { get; set; }
	//The construction method is omitted}

public class Data
{
    public int number { get; set; }
    public string day { get; set; }
	public List&lt;User&gt; userList { get; set; }
    public Token token { get; set; }
	//The construction method is omitted}

accomplish

var jObject = (json);

List<User> userList = <List<Shou>>(jObject["data"].ToString());
Token token = <Token>(jObject["token"]);
int number = (jObject["number"]);
string day = jObject["day"].ToString();

Data data = new Data(number,day,userList,token);

Summarize

This is the article about converting JSON to entity classes and List in C# and combining them. For more related content to convert JSON to entity classes and List, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!