【问题标题】:Getting datas from JSON file with Json.NET and create c-sharp file with using T4 Text Template使用 Json.NET 从 JSON 文件中获取数据并使用 T4 文本模板创建 c-sharp 文件
【发布时间】:2018-10-23 14:01:00
【问题描述】:

我想以编程方式创建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?)

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

【问题讨论】:

  • 更改您在其中反序列化 JSON 的类名。显然,这两个类具有相同的名称Subject

标签: c# json json.net t4


【解决方案1】:

要使用程序集中的任何引用(无论是像 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);
#>

【讨论】:

    【解决方案2】:

    至少,您需要为 Newtonsoft 命名空间添加一个额外的 &lt;#@ import namespace="Newtonsoft.Json" #&gt;

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      相关资源
      最近更新 更多