'Newtonsoft. JSON reports an error. How can I solve it?
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.
Solution 1:[1]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Serge |
