C# 使用JSON.NET从文件读取JSON-无法隐式转换类型';Newtonsoft.Json.Linq.JToken';至';字符串';

C# 使用JSON.NET从文件读取JSON-无法隐式转换类型';Newtonsoft.Json.Linq.JToken';至';字符串';,c#,json,json.net,C#,Json,Json.net,我正在尝试使用JSON.NET从文件中读取以下JSON { "SendTelemetry": true } 但是,我在解析数据时遇到了一些问题。这是我到目前为止所拥有的 public class SettingsStore { [JsonProperty] public bool SendTelemetry { get; set; } public dynamic ReadJsonFile(string filePath) { if (

我正在尝试使用JSON.NET从文件中读取以下JSON

{
    "SendTelemetry": true
}
但是,我在解析数据时遇到了一些问题。这是我到目前为止所拥有的

public class SettingsStore
{
    [JsonProperty]
    public bool SendTelemetry { get; set; }

    public dynamic ReadJsonFile(string filePath)
    {
        if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); }

        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException(string.Format(@"JSON settings file not found at [{0}]", filePath));
        }

        return JsonConvert.DeserializeObject(File.ReadAllText(filePath));
    }

    public void WriteJsonFile(string fileDirectory, string filePath, string json)
    {
        if (!Directory.Exists(fileDirectory))
        {
            Directory.CreateDirectory(fileDirectory);
        }

        File.WriteAllText(filePath, json);
    }
}

var rootDir = Environment.ExpandEnvironmentVariables(@"%localappdata%\LigerShark\SideWaffle\");
var filePath = Path.Combine(rootDir, "SideWaffle-Settings.json");
settings = new SettingsStore();
string json = settings.ReadJsonFile(filePath);
bool telemetry = JsonConvert.DeserializeObject<SettingsStore>(json.ToString()).SendTelemetry;
公共类设置存储
{
[JsonProperty]
公共布尔发送遥测{get;set;}
公共动态ReadJsonFile(字符串文件路径)
{
if(string.IsNullOrEmpty(filePath)){抛出新参数nullexception(“filePath”);}
如果(!File.Exists(filePath))
{
抛出新的FileNotFoundException(string.Format(@“在[{0}]上找不到JSON设置文件”,filePath));
}
返回JsonConvert.DeserializeObject(File.ReadAllText(filePath));
}
public void WriteJsonFile(字符串fileDirectory、字符串filePath、字符串json)
{
如果(!Directory.Exists(fileDirectory))
{
CreateDirectory(fileDirectory);
}
WriteAllText(文件路径,json);
}
}
var rootDir=Environment.ExpandEnvironmentVariables(@“%localappdata%\LigerShark\SideWaffle\”;
var filePath=Path.Combine(rootDir,“SideWaffle Settings.json”);
设置=新设置存储();
string json=settings.ReadJsonFile(filePath);
boolTelemetry=JsonConvert.DeserializeObject(json.ToString()).SendTelemetry;

当它到达反序列化json的点时,我得到错误
无法将类型“Newtonsoft.json.Linq.JToken”隐式转换为“string”。存在显式转换(是否缺少转换?
我已经阅读了JSON.NET文档,但我知道我缺少了一些东西。有人能帮我指出正确的方向吗?

看起来你在调用
jsonvert.DeserializeObject()
两次-一次在
ReadJsonFile()
中,一次在底部的代码中。您应该只需要调用一次,因此我不再调用
settings.ReadJsonFile()
,而是执行以下操作:

bool telemetry = JsonConvert.DeserializeObject<SettingsStore>(File.ReadAllText(filePath)).SendTelemetry;
bool telemetry=JsonConvert.DeserializeObject(File.ReadAllText(filePath)).SendTelemetry;

(当然,如果愿意,您可以将其移动到一个方法中,这样您就可以在
filePath
上添加检查)

看起来您在调用
jsonvert.DeserializeObject()
两次-一次在
ReadJsonFile()
中,一次在底部的代码中。您应该只需要调用一次,因此我不再调用
settings.ReadJsonFile()
,而是执行以下操作:

bool telemetry = JsonConvert.DeserializeObject<SettingsStore>(File.ReadAllText(filePath)).SendTelemetry;
bool telemetry=JsonConvert.DeserializeObject(File.ReadAllText(filePath)).SendTelemetry;

(当然,如果愿意,您可以将其移动到一个方法中,这样您就可以在
文件路径上添加回检查)

ReadJsonFile
返回一个
JToken
,因为您已经在反序列化对象

将代码更改为:

public class SettingsStore
{
    [JsonProperty]
    public bool SendTelemetry { get; set; }

    public static SettingsStore ReadJsonFile(string filePath)
    {
        if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); }

        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException(string.Format(@"JSON settings file not found at [{0}]", filePath));
        }

        return JsonConvert.DeserializeObject<SettingsStore>(File.ReadAllText(filePath));
    }

    public void WriteJsonFile(string fileDirectory, string filePath, string json)
    {
        if (!Directory.Exists(fileDirectory))
        {
            Directory.CreateDirectory(fileDirectory);
        }

        File.WriteAllText(filePath, json);
    }
}

ReadJsonFile
正在返回一个
JToken
,因为您已经在反序列化对象

将代码更改为:

public class SettingsStore
{
    [JsonProperty]
    public bool SendTelemetry { get; set; }

    public static SettingsStore ReadJsonFile(string filePath)
    {
        if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); }

        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException(string.Format(@"JSON settings file not found at [{0}]", filePath));
        }

        return JsonConvert.DeserializeObject<SettingsStore>(File.ReadAllText(filePath));
    }

    public void WriteJsonFile(string fileDirectory, string filePath, string json)
    {
        if (!Directory.Exists(fileDirectory))
        {
            Directory.CreateDirectory(fileDirectory);
        }

        File.WriteAllText(filePath, json);
    }
}

出现问题的原因是反序列化返回了一个
JToken
,因为您尚未强制转换结果

我建议稍微更改您的方法,让
ReadJsonFile
返回
SettingsStore
的实例,此方法为
static
(因为创建新对象时不需要实例)

像这样的东西看起来应该有用

public class SettingsStore
{
    [JsonProperty]
    public bool SendTelemetry { get; set; }

    public static SettingsStore ReadJsonFile(string filePath)
    {
        if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); }

        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException(string.Format(@"JSON settings file not found at [{0}]", filePath));
        }

        return JsonConvert.DeserializeObject<SettingsStore>(File.ReadAllText(filePath));
    }

    public void WriteJsonFile(string fileDirectory, string filePath, string json)
    {
        if (!Directory.Exists(fileDirectory))
        {
            Directory.CreateDirectory(fileDirectory);
        }

        File.WriteAllText(filePath, json);
    }
}

出现问题的原因是反序列化返回了一个
JToken
,因为您尚未强制转换结果

我建议稍微更改您的方法,让
ReadJsonFile
返回
SettingsStore
的实例,此方法为
static
(因为创建新对象时不需要实例)

像这样的东西看起来应该有用

public class SettingsStore
{
    [JsonProperty]
    public bool SendTelemetry { get; set; }

    public static SettingsStore ReadJsonFile(string filePath)
    {
        if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); }

        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException(string.Format(@"JSON settings file not found at [{0}]", filePath));
        }

        return JsonConvert.DeserializeObject<SettingsStore>(File.ReadAllText(filePath));
    }

    public void WriteJsonFile(string fileDirectory, string filePath, string json)
    {
        if (!Directory.Exists(fileDirectory))
        {
            Directory.CreateDirectory(fileDirectory);
        }

        File.WriteAllText(filePath, json);
    }
}