我有一个 python 脚本,它生成一个 json 文件,该文件进一步用于 C# 应用程序。这是创建导出的代码:
def put(data, filename):
with open(filename, 'w') as outfile:
json.dump(data, outfile, indent=4, ensure_ascii=False, encoding='utf8')
然后在 C# 应用程序中,我尝试通过以下方式读取它:
public static string readFile(string filename)
{
using (StreamReader r = new StreamReader(filename))
{
return r.ReadToEnd();
}
}
//different file
string json_result = funcs.readFile(out_file);
VirtualMachine newvm = JsonConvert.DeserializeObject<MyClass>(json_result);
问题是json_result
包含\r\n
每个换行符处的字符并且无法反序列化 json。
我试图检查由 python 脚本生成的编码,根据 SublimeText 是u'Undefined'
如何使 python 为 C# 正确编码文件或使 C# 以正确的编码加载它?