相关疑难解决方法(0)

JSON.NET:在不重新解析的情况下缩小/格式化内容

是否可以使用Newtonsoft JSON.NET库缩小/格式化JSON字符串而不强制系统重新解析代码?这就是我的方法:

public async Task<string> Minify(string json)
{
    // TODO: Some way to do this without a re-parse?
    var jsonObj = await JsonOpener.GetJsonFromString(json);
    return jsonObj.ToString(Formatting.None);
}

public async Task<string> Beautify(string json)
{
    // TODO: Some way to do this without a re-parse?
    var jsonObj = await JsonOpener.GetJsonFromString(json);
    return FormatJson(jsonObj);
}

private string FormatJson(JToken input)
{
    // We could just do input.ToString(Formatting.Indented), but this allows us
    // to take advantage of JsonTextWriter's formatting options.
    using (var stringWriter = new StringWriter(new StringBuilder())) …
Run Code Online (Sandbox Code Playgroud)

c# json json.net

8
推荐指数
1
解决办法
746
查看次数

标签 统计

c# ×1

json ×1

json.net ×1