The code is written like this:
public class Value
{
public string Remote_server { get; set; }
public string Game_version { get; set; }
}
public class RootObject
{
public Value[] @Config { get; set; } // Json字段名
}
public void Window_ContentRendered(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
string conf = path @"mchmr\config.json";
var objJSON = JsonConvert.DeserializeObject<RootObject>(conf);
string url = objJSON.Config[0].Remote_server;
Data_1.Text = url;
This is the content of the JSON file:
{
"Config": [
{
"Remote_server": "https://www.test.com/mchr/server_config.json",
"Game_version": "0.9"
}
]
}
This is the error content of vs:
Newtonsoft.Json.JsonReaderException:“Unexpected character encountered while parsing value: D. Path '', line 0, position 0.”
The error code is:
var objJSON = JsonConvert.DeserializeObject<RootObject>(conf);
My English is not good, please forgive me.
CodePudding user response:
this is what I would be using
string json = string.Empty;
using (StreamReader r = new StreamReader( @jsonfilepath)) json = r.ReadToEnd();
var objJSON = JsonConvert.DeserializeObject<RootObject>(json);
CodePudding user response:
Here is an alternate way to do it:
var path = AppDomain.CurrentDomain.BaseDirectory;
var conf = path @"mchmr\config.json";
// You missed this line probably.
var jsonStr = string.Join(string.Empty, File.ReadAllLines(conf));
var obj = JsonConvert.DeserializeObject<RootObject>(jsonStr);