问题
I'm trying to use DeserializeObject
from the NewtonSoft library but I encoutered a problem that bothers me a lot.
I'm quite new with that Library so to understand it better I took a look at the following example from the official website.
Using that ressource, I modified it to fit my needs, here is my class :
public class Project
{
[JsonProperty ("team")]
public string Team{ get; set; }
[JsonProperty("details")]
public string Details { get; set;}
}
Here is the code :
Project project = new Project();
string json_string = File.ReadAllText(@"C:\file.json", Encoding.UTF8);
project = JsonConvert.DeserializeObject<Projets>(json_string);
And here is the .json file template :
{
"Team": "nameOfTeam",
"Details": [
{
"detail1": "Unknown",
"detail2": "Unknown"
}
]
}
Problem is, there is an exception during the run-time with the following message
Unexpected character encountered while parsing value: [. Path 'Details', line X, position XX.
I don't understand how the character "[" could be a problem here, am I missing something ?
回答1:
Details
is a string in your C# class, while it's an array in the JSON object. Either make it a string in JSON, or make the property an IList<string>
instead.
来源:https://stackoverflow.com/questions/48426590/newtonsoft-json-jsonreaderexception-does-not-recognize-left-square-bracket