C#文本转换为Json格式
2021-05-22 09:31
标签:div 转换 indent iter index csharp return ons arp C#文本转换为Json格式 标签:div 转换 indent iter index csharp return ons arp 原文地址:https://www.cnblogs.com/LCLBook/p/11196362.htmlprivate
string
ConvertJsonString(
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;
}
}
下一篇:WPFの触发器详解