Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've got this code to try to open a .json file and read it:

C#
[Route("{unit}/{begindate}")]
public string Get(string unit, string begindate)
{
    string _unit = unit;
    string _begindate = String.Format("{0}01", PlatypusWebReportsConstsAndUtils.HyphenizeYYYYMM(begindate));
    string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/");
    // semi-hardcoded file name for now
    string jsonFilename = string.Format("PoisonToe_{0}_{1}.json", _unit, _begindate);
    string fullPath = Path.Combine(appDataFolder, jsonFilename);
    JObject platypusComplianceJson = JObject.Parse(File.ReadAllText(fullPath));
    . . .


On the last line, I get:

Newtonsoft.Json.JsonReaderException was unhandled by user code
HResult=-2146233088
Message=Error reading JObject from JsonReader. Current JsonReader item is not an object:


Based on what I read here, I thought this would be the way to do it. What am I doing wrong?
Posted
Comments
Afzaal Ahmad Zeeshan 31-Dec-15 15:51pm    
Is it an array? :-)
B. Clay Shannon 31-Dec-15 15:55pm    
Is what an array? The .json file?
George Jonsson 31-Dec-15 23:17pm    
Have you checked that the return value from File.ReadAllText(fullPath) actually contains data.
That's the only thing I can see that could go wrong.
B. Clay Shannon 1-Jan-16 0:19am    
It's the same path I used to save the file in the first place, and is there, and has content, so...
George Jonsson 1-Jan-16 0:55am    
As I said, that was the only thing I could think of.
I just tried the code on a very simple JSON file and it works fine.
You probably have an error in the file you are reading.
Try something simple like:
{
"Hello":"World"
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900