方法
string jsonfile = Path.Combine(Directory.GetCurrentDirectory(), "Configuration", "TaxSeal.json");//JSON文件路径
using (StreamReader file = File.OpenText(jsonfile))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
var baseimgjson = JsonConvert.DeserializeObject<SSD>(JToken.ReadFrom(reader).ToString());//序列化
foreach (var item in baseimgjson.Data)
{
if (item.Name == array.TaxBureau)
{
var ss = item.Code;//赋值
}
}
}
}
json文件
{
"data": [
{
"Name": "安徽",
"Code": "02“,
},
{
"Name": "湖南",
"Code": "01“,
}
]
}
类文件
public class Invoiceimg
{
/// <summary>
/// 名称
/// </summary>
public string? Name { get; set; }
/// <summary>
/// base编码
/// </summary>
public string? Code { get; set; }
}
public class SSD
{
/// <summary>
/// 名称
/// </summary>
public Invoiceimg[] Data { get; set; }
}