我正在使用 JavaScriptSerializer 将对象序列化为 JSON 格式的文件。但是结果文件没有可读的格式。如何允许格式化以获得可读文件?
问问题
23428 次
4 回答
25
这是我的解决方案,它不需要使用 JSON.NET,并且比 Alex Zevzhik 链接的代码更简单。
using System.Web.Script.Serialization;
// add a reference to System.Web.Extensions
public void WriteToFile(string path)
{
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(this);
string json_pretty = JSON_PrettyPrinter.Process(json);
File.WriteAllText(path, json_pretty );
}
这是格式化程序
class JSON_PrettyPrinter
{
public static string Process(string inputText)
{
bool escaped = false;
bool inquotes = false;
int column = 0;
int indentation = 0;
Stack<int> indentations = new Stack<int>();
int TABBING = 8;
StringBuilder sb = new StringBuilder();
foreach (char x in inputText)
{
sb.Append(x);
column++;
if (escaped)
{
escaped = false;
}
else
{
if (x == '\\')
{
escaped = true;
}
else if (x == '\"')
{
inquotes = !inquotes;
}
else if ( !inquotes)
{
if (x == ',')
{
// if we see a comma, go to next line, and indent to the same depth
sb.Append("\r\n");
column = 0;
for (int i = 0; i < indentation; i++)
{
sb.Append(" ");
column++;
}
} else if (x == '[' || x== '{') {
// if we open a bracket or brace, indent further (push on stack)
indentations.Push(indentation);
indentation = column;
}
else if (x == ']' || x == '}')
{
// if we close a bracket or brace, undo one level of indent (pop)
indentation = indentations.Pop();
}
else if (x == ':')
{
// if we see a colon, add spaces until we get to the next
// tab stop, but without using tab characters!
while ((column % TABBING) != 0)
{
sb.Append(' ');
column++;
}
}
}
}
}
return sb.ToString();
}
}
于 2012-11-06T22:02:20.057 回答
19
我还希望能够在不依赖第三方组件的情况下格式化 JSON。Mark Lakata 的解决方案运行良好(感谢 Mark),但我希望括号和制表符与 Alex Zhevzhik 链接中的一样。所以这里有一个经过调整的 Mark 代码,可以这样工作,以防其他人想要它:
/// <summary>
/// Adds indentation and line breaks to output of JavaScriptSerializer
/// </summary>
public static string FormatOutput(string jsonString)
{
var stringBuilder = new StringBuilder();
bool escaping = false;
bool inQuotes = false;
int indentation = 0;
foreach (char character in jsonString)
{
if (escaping)
{
escaping = false;
stringBuilder.Append(character);
}
else
{
if (character == '\\')
{
escaping = true;
stringBuilder.Append(character);
}
else if (character == '\"')
{
inQuotes = !inQuotes;
stringBuilder.Append(character);
}
else if (!inQuotes)
{
if (character == ',')
{
stringBuilder.Append(character);
stringBuilder.Append("\r\n");
stringBuilder.Append('\t', indentation);
}
else if (character == '[' || character == '{')
{
stringBuilder.Append(character);
stringBuilder.Append("\r\n");
stringBuilder.Append('\t', ++indentation);
}
else if (character == ']' || character == '}')
{
stringBuilder.Append("\r\n");
stringBuilder.Append('\t', --indentation);
stringBuilder.Append(character);
}
else if (character == ':')
{
stringBuilder.Append(character);
stringBuilder.Append('\t');
}
else if (!Char.IsWhiteSpace(character))
{
stringBuilder.Append(character);
}
}
else
{
stringBuilder.Append(character);
}
}
}
return stringBuilder.ToString();
}
于 2014-05-23T12:09:14.693 回答