Reputation: 1037
I am working on reading a json sting in my C# code and run into the error "Illegal characters in path". I did a check on the json string if it adheres to the standards and no issue there. But, when I try to read this data in my code inexplicably run into this error. I'm using this to process my json string.
JSON:
[{
"Id": 1,
"FirstName": "Jason1",
"LastName": "Test1",
"Email": "[email protected]",
"Eligible": true,
"InsertLogtime": "2022-02-21T00:51:59.917",
"Comment": null
},
{
"Id": 2,
"FirstName": "Jason2",
"LastName": "Test2",
"Email": "[email protected]",
"Eligible": true,
"InsertLogtime": "2022-02-21T00:51:59.917",
"Comment": null
}
]
C#:
string jstring = @"[{
"Id": 1,
"FirstName": "Jason1",
"LastName": "Test1",
"Email": "[email protected]",
"Eligible": true,
"InsertLogtime": "2022-02-21T00:51:59.917",
"Comment": null
},
{
"Id": 2,
"FirstName": "Jason2",
"LastName": "Test2",
"Email": "[email protected]",
"Eligible": true,
"InsertLogtime": "2022-02-21T00:51:59.917",
"Comment": null
}
]";
using (Stream stream = File.OpenRead(jstring))
{
}
Error:
Upvotes: 0
Views: 1038
Reputation: 50190
Your error has nothing to do with parsing json
File.OpenRead
expects a file name. You have passed in a json string, this is not a valid file name, hence the error
You should parse the string directly
Upvotes: 3
Reputation: 43880
you can just parse your json string
using Newtonsoft.Json;
var jArray=JArray.Parse(jstring);
var firstName= (string) jArray[0]["FirstName"];
Upvotes: 0