I'm getting the error "Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '', line 1, position 1.'"
My json file content is: { "employees": [] }
My code:
var jsonStr = System.IO.File.ReadAllText(contentRootPath "/Resources/Employees.json");
if (string.IsNullOrWhiteSpace(jsonStr))
{
return null;
}
var json = JsonConvert.DeserializeObject<string>(jsonStr);
I've verified in the debugger that the jsonStr content is: "{ \"employees\": [] }"
CodePudding user response:
You are trying to deserialize your JSON object
as a string. You need to make a class / struct with an employees
field and deserialize it into that.
Or deserialize it into a dynamic object and navigate the tree manually.
CodePudding user response:
Accepting that employees is an array of string, try this
var json="{\"employees\": []}";
var jD = JsonConvert.DeserializeObject<Root>(json);
List<string> employees=jD.Employees.ToList();
public class Root
{
[JsonProperty("employees")]
public string[] Employees {get; set;}
}
test
json= JsonConvert.SerializeObject(jD);
output
{"employees":[]}