27

オブジェクトをファイルにシリアル化して JSON 形式にするために JavaScriptSerializer を使用しています。しかし、結果ファイルには読み取り可能なフォーマットがありません。フォーマットを許可して読み取り可能なファイルを取得するにはどうすればよいですか?

4

4 に答える 4

35

JSON.NETシリアライザーを使用できます。JSONフォーマットをサポートしています

string body = JsonConvert.SerializeObject(message, Formatting.Indented);

Yonは、NuGetを介してこのパッケージをダウンロードできます。

于 2011-05-04T12:22:14.443 に答える
25

これは、JSON.NET を使用する必要がなく、Alex Zhevzhik によってリンクされたコードよりも単純な私のソリューションです。

    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 に答える
13

JSONシリアライザーの出力をフォーマットするための組み込みツールがないようです。
これが起こった理由は、ネットワーク経由で送信するデータを最小限に抑えているためだと思います。

コードに書式設定されたデータが必要ですか? それとも、デバッグ中に JSON を分析したいですか?

このような機能を提供するオンライン サービスは多数あります: 12。またはスタンドアロン アプリケーション: JSON ビューアー

ただし、アプリケーション内で書式設定が必要な場合は、自分で適切なコードを記述できます。

于 2011-05-04T09:46:26.200 に答える