using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;//要引入这个包哦
namespace JsonTest
{
class Program
{
static void Main(string[] args)
{
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { name = "周润发",sex = "男" , address = "武汉",sorce = new sorce { chinese ="chin12",english="eng12",math = "mat12"} });
employees.Add(new Employees { name = "李素净", sex = "女", address = "兰州", sorce = new sorce { chinese = "chin13", english = "eng13", math = "mat13" } });
employees.Add(new Employees { name = "周智慧", sex = "男", address = "武汉", sorce = new sorce { chinese = "chin14", english = "eng14", math = "mat14" } });
var jsonstr = JsonConvert.SerializeObject(employees);
Console.WriteLine(jsonstr);
var emp = JsonConvert.DeserializeObject<List<Employees>>(jsonstr);
foreach (var item in emp)
{
Console.WriteLine(item.name+" "+item.sex+" "+item.address+" "+item.sorce.chinese+" "+item.sorce.english+" "+ item.sorce.math);
}
Console.ReadKey();
}
}
public class Employees
{
public string name;
public string sex;
public string address;
public sorce sorce;
}
public class sorce
{
public string chinese;
public string english;
public string math;
}
}
Conversión entre tipo de datos JSON y tipo de objeto
Supongo que te gusta
Origin blog.csdn.net/qq_39072819/article/details/95363841
Recomendado
Clasificación