开发者

JSON File to Byte[], need to go Byte[] to JSON string

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜