10

I have a file in visual studio with the following contents:"{"Name":"Pete"}" If I read the file with the following code it appears to create a string with the original value:

byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
string jsonResponse = System.Text.Encoding.UTF8.GetString(byteArray);

However, the string is actually different to the version that exists if I use the following code:

string jsonResponse = "{\"Name\":\"Pete\"}";

Why? (The reason I think it is different is because when I pass each version to a json deserializer it behaves differently)

Thanks.

5
  • In what way is the string different? Commented Apr 19, 2011 at 11:55
  • what exactly are the file contents? can you post that as a code-block without any surrounding quotes (but with any quotes that are actually in the file), so we can be exactly sure of the contents? Also - I assume it was saved with UTF8? Commented Apr 19, 2011 at 11:56
  • This is what is in the file:{"Contact":"Pete"} Basically it starts with a bracket, and ends with a bracket and was created with VS2008. Commented Apr 19, 2011 at 11:58
  • 1
    Then the solution is easy: Your file contains "Contact", whereas your string contains "Name". ;-) Commented Apr 19, 2011 at 11:59
  • @Heinzi - well, sort of, but the point is that the json deserializer I'm using is complaining about the version that is read from the file. When I view it in the debug window the string looks like this: {\"Contact\":\"Pete\"} (There is a small dot just before the opening bracket which is not showing up in this comment Commented Apr 19, 2011 at 12:00

2 Answers 2

8

Given your final comment in the question, I suspect the problem is that you've got a byte-order mark at the start of the file. Try loading the file like this instead:

string jsonResponse = File.ReadAllText(filePath);

I believe that will strip the BOM for you. Alternatively, you could try explicitly trimming it yourself:

jsonResponse = jsonResponse.TrimStart('\feff');
Sign up to request clarification or add additional context in comments.

Comments

4

My guess would be that you have a terminating newline in your file.

You can easily verify if two strings have the same content in C# by just comparing them with a == b.

Here's a short code sample that might help you identify the problem. The strings are output surrounded by < >, which should help you identify surrounding whitespace (which, by the way, can be removed using String.Trim).

byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
string fromFile = System.Text.Encoding.UTF8.GetString(byteArray);
string fromString = "{\"Name\":\"Pete\"}";

if (fromFile == fromString) {
    Console.WriteLine("Strings are the same.");
} else {
    Console.WriteLine("Strings are different!");
    Console.WriteLine("fromFile:   <" + fromFile + ">");
    Console.WriteLine("fromString: <" + fromString + ">");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.