在网上找到了一些格式化json数据的方法,会让json数据看起来更简洁,而不是从西到东一条线
需要的引用
using Newtonsoft.Json;
static public string ConvertJsonString(string json)
{
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(json);
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 = 2,
//key: value (冒号后的字符)
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return json;
}
}