0

I am trying to get the value of a key in my JSON data however I am receiving a question mark as response.

My config.json file:

{
    currency_sign: "£"
}

My code:

public static void readConfig()
{
    var file = File.ReadAllText("config.json");
    var file2 = JObject.Parse(file);

    string currency = file2.SelectToken("currency_sign").Value<string>();
    Console.WriteLine("Currency: " + currency);
}

Output of my code:

Currency: ?

I have also tried encoding the string:

byte[] bytes = Encoding.Default.GetBytes(currency);
currency = Encoding.UTF8.GetString(bytes);

However the output was exactly the same.

Why is my code returning ? instead of £ and how can I fix this?

  • 1
    Would specifying encoding parameter such as `Encoding.UTF8` for `File.ReadAllText` help? – shahkalpesh Jun 14 '20 at 16:59
  • Does this answer your question? [Currency format for display](https://stackoverflow.com/questions/4842332/currency-format-for-display) – Jawad Jun 14 '20 at 17:57
  • Please see https://www.joelonsoftware.com/articles/Unicode.html. Then [specify the encoding](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=netcore-3.1#System_IO_File_ReadAllText_System_String_System_Text_Encoding_) of your file in the `ReadAllText` call. – GSerg Jun 14 '20 at 19:46

1 Answers1

-1

Try setting console output encoding to UTF8 before your Console.WriteLine statement:

Console.OutputEncoding = System.Text.Encoding.UTF8;
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • @Fluffy what OS? What system language? – Guru Stron Jun 14 '20 at 17:08
  • 1
    @Fluffy also what output do you get with simple `Console.WriteLine("£")`? – Guru Stron Jun 14 '20 at 17:09
  • Windows 10 Pro, C# .Net Console Application –  Jun 14 '20 at 17:15
  • I'm guessing the value itself has to be encoded as JSON may be having trouble reading it. –  Jun 14 '20 at 17:23
  • Also I have tested this in Python, the only difference is that it outputted `£` correctly. –  Jun 14 '20 at 17:29
  • 1
    @Fluffy try writing the whole file/line with `currency_sign` to console. – Guru Stron Jun 14 '20 at 17:29
  • Sure, I did `Console.WriteLine(file2);` and the output was: `{ "currency_sign": "?" }` –  Jun 14 '20 at 17:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215958/discussion-between-guru-stron-and-fluffy). – Guru Stron Jun 14 '20 at 18:47
  • It is unhelpful for future readers that this answer is marked as accepted, given that it cannot be correct due to https://stackoverflow.com/questions/62375592/getting-the-value-of-a-pound-sign-%c2%a3-in-json-returns-a-question-mark#comment110317032_62375672. If you found an actual solution in the chat, please edit your answer to reflect that. – GSerg Jun 15 '20 at 09:44