0

I created a json file inside of Visual Studio and added it as a resource to my console app. For some reason, it gets added as a byte[] which is fine. I read it like this:

string str = Encoder.UTF8.GetString(Properties.Resources.myJson);

json looks okay in the debugger, but when I call JObject.Parse(str) it complains about a parsing error. I tracked this down to it appears there is a junk byte at the beginning. I thought UTF8 was supposed to strip off any encoding bytes? Can I always assume there will be one junk character? If I do a SubString(1), then JObject.Parse works fine.

Or is there a safer way to get the myJson byte[] into the JObject?

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • You can change the properties in Resources to make it a string. – SLaks Feb 16 '18 at 01:45
  • 2
    Sounds like a [Byte Order Mark](https://en.wikipedia.org/wiki/Byte_order_mark). Are you sure you can't set the json to be a string resource? – stuartd Feb 16 '18 at 01:45
  • Instead of an `Encoding`, use a `StreamReader` as is shown [here](https://stackoverflow.com/a/2915239/3744182). – dbc Feb 16 '18 at 01:52
  • @SLaks how do you do that? It seems like it does that automatically for text files, but not for other extensions. Nothing in the Properties window. – SledgeHammer Feb 16 '18 at 03:09
  • @SLaks -- nm... figured out how to do it... sneaky undocumented F4 key :). – SledgeHammer Feb 16 '18 at 03:15

0 Answers0