I have JSON in a text file, and I read the bytes of the file into an array:
byte[] data = File.ReadAllBytes(filename);
Now, I want to get a string that contain开发者_C百科s the JSON data that was in the original file, but I only have the data byte array available.
Any help would be appreciated.
Thanks.
What about using File.ReadAllText instead?
Anyway, you can convert a Byte[] to a String using Encoding.UTF8.GetString(data)
You need to setup an encoding and then convert the bytes to a string, like so:
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString(myByteArray );
Although, if your goal is simply to read the JSON into a string, then everyone else's answer is right. Just use File.ReadAllText
Why not use File.ReadAllText
instead? That'll give you a string off the bat.
精彩评论