异步调用获取JSON数据时非常不直观,每次都需要格式化一次,才能直观的看到数据集合的结构,现在需要实现输出带缩进的格式。
在浏览器的查看源文件中已经实现格式化,如果是页面使用,可以直接赋值给textarea同样能以格式化后的效果显示。
public static String Format(string text) { string result = string.Empty; try { JsonSerializer serializer = new JsonSerializer(); JsonReader reader = new JsonTextReader(new StringReader(text)); object obj2 = serializer.Deserialize(reader); if (obj2 != null) { StringWriter textWriter = new StringWriter(); JsonWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ‘ ‘ }; serializer.Serialize(jsonWriter, obj2); result = textWriter.ToString(); } } catch (Exception exception) { LogHelper.Write(exception.Message); result = exception.Message; } return result; }
原文:http://www.cnblogs.com/shya/p/4416116.html