首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Json.NET从JSON文件中获取数据,使用T4文本模板创建c-sharp文件

使用Json.NET从JSON文件中获取数据,使用T4文本模板创建c-sharp文件
EN

Stack Overflow用户
提问于 2018-10-23 22:01:01
回答 2查看 1.8K关注 0票数 2

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

代码语言:javascript
复制
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文件的内容:

代码语言:javascript
复制
{
    "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文件中的数据。为此,我使用了这样的类:

代码语言:javascript
复制
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; }
}

最后,为了获取数据,我使用如下代码:

代码语言:javascript
复制
var json = JsonConvert.DeserializeObject<SubjectList>(File.ReadAllText("D:\\Colin\\_test\\inf.json"));

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

代码语言:javascript
复制
<#@ 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文件而不会出现任何问题?

EN

回答 2

Stack Overflow用户

发布于 2019-04-15 22:53:20

特别是在您的情况下,尝试直接使用程序集名称添加引用的程序集。顺便说一句,相对于文本模板本身,读入json文件也很好。示例:

代码语言:javascript
复制
<#@ 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);
#>
票数 4
EN

Stack Overflow用户

发布于 2018-12-20 01:28:15

至少,您需要为Newtonsoft名称空间添加一个额外的<#@ import namespace="Newtonsoft.Json" #>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52950990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档