首页 > 解决方案 > 使用 Json.NET 从 JSON 文件中获取数据并使用 T4 文本模板创建 c-sharp 文件

问题描述

我想以Subject.cs编程方式创建文件。为此,当我在网上搜索时,我看到我可以使用 T4 文本模板。现在,我想使用 Json.NET 从 JSON 文件中获取数据并将Root值设置为属性,如下所示:

namespace Library
{
    class Subject
    {
        public static int forensic_medicine { get; set; }
        public static int family_law { get; set; }
        public static int constitutional_law { get; set; }
        public static int obligations_law { get; set; }
    }
}

这是 JSON 文件内容:

{
    "Subjects": [
        {
            "Root": "forensic_medicine",
            "Name": "Forensic Medicine",
            "StrID": "FMD",
            "RowID": 746
        },
        {
            "Root": "family_law",
            "Name": "Family Law",
            "StrID": "FML",
            "RowID": 1239
        },
        {
            "Root": "constitutional_law",
            "Name": "Constitutional Law",
            "StrID": "CNL",
            "RowID": 996
        },
        {
            "Root": "obligations_law",
            "Name": "Obligations Law",
            "StrID": "OBL",
            "RowID": 1672
        }
    ],
    "is_restart": 0,
    "book_path": "D:\\Colin\\_books\\",
    "thesis_path": "D:\\Colin\\_thesises\\",
    "full_screen": false
}

我认为,我必须使用SubjectListSubject类来从 JSON 文件中读取和获取数据。为此,我正在使用这样的类:

public class SubjectList
{
    public List<Subject> Subjects { get; set; }
}

public class Subject
{
    public string StrID { get; set; }
    public string Name { get; set; }
    public string Root { get; set; }
    public int RowID { get; set; }
}

最后,为了获取数据,我使用这样的代码:

var json = JsonConvert.DeserializeObject<SubjectList>(File.ReadAllText("D:\\Colin\\_test\\inf.json"));

在文本模板文件 ( Subjects.tt) 中,代码如下:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)Test\bin\Debug\Newtonsoft.Json.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
namespace Library
{
    public class Subject
    {
        <#
            var json = JsonConvert.DeserializeObject<SubjectList>(File.ReadAllText("D:\\Colin\\_test\\inf.json"));
        #>
    }
}

我没有做任何其他事情就收到以下错误。

Compiling transformation: The name 'JsonConvert' does not exist in the current context

Compiling transformation: The type or namespace name 'SubjectList' could not be found (are you missing a using directive or an assembly reference?)

我想我必须在文件中使用SubjectListSubjectSubjects.tt,但我不知道我是怎么做的。更重要的是,如何以Subject.cs编程方式创建文件而不会出现任何问题?

标签: c#jsonjson.nett4

解决方案


若要使用程序集中的任何引用(无论是像 Newtonsoft.Json 这样的 NuGet 包还是项目输出),您必须添加导入程序集指令。具体而言,尝试通过直接使用程序集名称来添加引用的程序集。附带说明一下,阅读相对于文本模板本身的 json 文件也很好。例子:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ output extension=".cs" #>
<#
    string templateFileName = Path.GetFileNameWithoutExtension(this.Host.TemplateFile);
    string settingsPath = this.Host.ResolvePath($"{templateFileName}.json");
    string settingsJson = File.ReadAllText(settingsPath);
    var datasource = JObject.Parse(settingsJson);
#>

推荐阅读