Newtonsoft.Json.JsonReaderException does not recognize left square bracket “[”

家住魔仙堡 提交于 2021-02-17 07:10:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!