首页 > 技术文章 > 字符编码

YaoFrankie 2018-09-15 12:40 原文

一.问题描述:C#读取json文件中的中文时,出现以下乱码。

õ�տ��ٷ���Ӫ����Ʒ����

1.部分代码展示

var filePath = @"C:\Users\15900\source\repos\Test1\Test1\test_utf8.json";
FileStream pdJson = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (StreamReader sr = new StreamReader(pdJson))
{
    string str = sr.ReadToEnd();
    Console.WriteLine(str);
    Console.ReadLine();
}

 

二.解决方案

1.Encoding.Default、Encoding.UTF8

...
using (StreamReader sr = new StreamReader(pdJson, Encoding.UTF8))
...

 

 

2.文件字符编码

 

 

三.总结 

阮一峰.字符编码笔记:ASCII,Unicode 和 UTF - 8  -->  http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html

1.ascii:Amerian Standard Code for Information Interchange

2.unicode: 万国码

3.utf-8:是互联网上使用最广的一种unicode的实现方式

 

Q:

1.System.Text.Encoding, Version = 4.0.10.0 does't have Default Property.

推荐阅读