1

I have txt file that contains json formatted data. file content looks like:

Defter.txt

My conversion code is;

byte[] fileContent =  // Read txt file

JournalFake journalFake = JsonConvert.DeserializeObject<JournalFake>(Encoding.UTF8.GetString(fileContent), new LineNumberConverter());

If I observe value of Encoding.UTF8.GetString(fileContent) with Quick Watch, value of Encoding.UTF8.GetString(fileContent) is below;

value of Encoding.UTF8.GetString(fileContent)

When I copy this json data to jsoneditoronline.org to check if json is valid, it gives me an error like below(a dot appears at the head of json file).

json data on jsoneditoronline.org

If I run JournalFake journalFake = JsonConvert.DeserializeObject<JournalFake>(Encoding.UTF8.GetString(fileContent), new LineNumberConverter()); statement, it throws an error: unexpected character encountered while parsing value . path '' line 0 position 0

What goes wrong? Any help?

Mehmet Ince
  • 4,059
  • 12
  • 45
  • 65
  • 1
    I lean toward a byte-order mark, but I didn't think `UTF8Encoding.GetString` returned one... – T.J. Crowder Jul 19 '16 at 10:50
  • 1
    There must be a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) in the `byte[] fileContent` file. `Encoding.UTF8.GetString(fileContent)` will not add a BOM but it will not strip it either if present in the file. Instead, use a `StreamReader` to read the file, it will process it correctly. See also https://stackoverflow.com/questions/1317700/strip-byte-order-mark-from-string-in-c-sharp – dbc Jul 19 '16 at 11:12
  • Probable duplicate: https://stackoverflow.com/questions/11701341/encoding-utf8-getstring-doesnt-take-into-account-the-preamble-bom – dbc Jul 19 '16 at 11:13
  • use BodyStream property of your Message instead of Body if you are using MSMQ. – Kubi Sep 21 '16 at 05:10

0 Answers0