I'm trying to read the following JSON from a file using JSON.NET.
{
"SendTelemetry": true
}
However, I'm having some issues with parsing the data. Here's what I have so far.
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;
When it gets to the point of deserializing the json I get the error Cannot implicitly convert type 'Newtonsoft.Json.Linq.JToken' to 'string'. An explicit conversion exists (are you missing a cast?) I've been going through the JSON.NET documentation but I know I'm missing something. Can someone please help point me in the right direction?