本文最后更新于 2024-05-07,文章可能存在过时内容,如有过时内容欢迎留言或者联系我进行反馈。
效果展示 {#%E6%95%88%E6%9E%9C%E5%B1%95%E7%A4%BA}
代码示例 {#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B}
using System.IO;
using Newtonsoft.Json;
`private string ConvertJsonString(string str)
{
if (!string.IsNullOrEmpty(str))
{
try
{
//格式化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();
}
}
catch (Exception ex)
{
throw ex;
}
}
return str;
}
`