0

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:

enter image description here

4
  • please show the code thats reading the JSON Commented Apr 7, 2022 at 23:05
  • In your screenshot the path you are using is jstring. I think this is what is causing the issue. DOCS: path String The file to be opened for reading. Commented Apr 7, 2022 at 23:05
  • 2
    File.OpenRead expects a file name, not a json string Commented Apr 7, 2022 at 23:06
  • @pm100, I'm posting just the snippet. I am passing the name of the file in my code. Commented Apr 8, 2022 at 0:52

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

you can just parse your json string

using Newtonsoft.Json; var jArray=JArray.Parse(jstring); var firstName= (string) jArray[0]["FirstName"]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.