2021年1月26日星期二

c# Save korean string to file, not utf form string

There is a class, Data. Data has a member, "name" which contains Korean string.

I make json string with System.Text.Json

JsonSerializer.Serialize<Data>(data, opt);  

and make json file

File.WriteAllText(filePath, jsonstring, Encoding.UTF8);  

part of json is

"name": "\uC0BC\uC131\uC804\uAE30",

but Json string what I want is

"name": "삼성전기",

I tried

File.WriteAllText(filePath, jsonstring, Encoding.Unicode)  File.WriteAllText(filePath, jsonstring, Encoding.Default)  

but the contents of the file are in this format \uxxxx.

How can I save Korean string to file as it is?

(As a reference) When I try to find solution (for C#) I found out function(json.dumps) in Python I can get the results what I want by using it

(example)

import json  dict = {'555': '123'}  with open('file.txt', 'w', encoding='UTF-8') as file:  file.write(json.dumps(dict))  
https://stackoverflow.com/questions/65904154/c-sharp-save-korean-string-to-file-not-utf-form-string January 26, 2021 at 11:25PM

没有评论:

发表评论