//格式化Json 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; } }
需要引用命名空间
using Newtonsoft.Json;
标签:格式化,C#,textWriter,json,obj,str,new From: https://www.cnblogs.com/tiancaige/p/18427870