首页 前端知识 C# JSON转为实体类和List,以及结合使用

C# JSON转为实体类和List,以及结合使用

2024-04-29 12:04:17 前端知识 前端哥 887 505 我要收藏

引用

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

JSON转实体类

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 = JsonConvert.DeserializeObject<Person>(jsonStr);

JSON转List

方法一

json

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

实现

JObject obj = JObject.Parse(json);

//Shou是于json中数组一个元素所对应的类
List<Shou> root = JsonConvert.DeserializeObject<List<Shou>>(obj["data"].ToString());

方法二

json

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

两个类

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; }
	//省略了构造方法
}

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string ProfilePicture { get; set; }
	//省略了构造方法
}

实现

var jObject = JObject.Parse(json);
var userPropery = jObject["user"] as JArray;
List<User> userList= new List<User>();

foreach (var property in userPropery )
{
    var propertyList = JsonConvert.DeserializeObject<List<user>>(property.ToString());
	//List集合中的AddRange方法和Add方法的不同之处在于,Add方法为在集合末尾添加单个元素对象,而AddRange方法可以为添加整个目标list集合。
    userList.AddRange(propertyList);
}

结合使用

json

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

两个类

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; }
	//省略了构造方法
}

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string ProfilePicture { get; set; }
	//省略了构造方法
}

public class Data
{
    public int number { get; set; }
    public string day { get; set; }
	public List<User> userList { get; set; }
    public Token token { get; set; }
	//省略了构造方法
}

实现

var jObject = JObject.Parse(json);

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

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

转载请注明出处或者链接地址:https://www.qianduange.cn//article/6361.html
标签
c#NodeList
评论
发布的文章

HTML5本地存储

2024-05-06 09:05:10

HTML5和CSS3新特性

2024-04-16 17:04:36

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!