C# winform格式化Json
2021-03-06 04:29
标签:rgs event info tchar button null reader string 格式化json //格式化Json { } C# winform格式化Json 标签:rgs event info tchar button null reader string 格式化json 原文地址:https://www.cnblogs.com/tuxer/p/12869126.html
private string ConvertStringToJson(string str)
//格式化json字符串
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ‘ ‘
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}
private void button16_Click(object sender, EventArgs e)
{
string str = textBox2.Text;
textBox2.Text = ConvertStringToJson(str);