Minify a json string using .NET

核能气质少年 提交于 2020-08-05 19:22:11

问题


How can an existing json string be cleaned up/minfied? I've seen regexes being used. Any other (maybe more efficient) approach?


回答1:


Install-Package Newtonsoft.Json

Just parse it and then serialize back to JSON:

var jsonString = "  {  title: \"Non-minified JSON string\"  }  ";
var obj = JsonConvert.DeserializeObject(jsonString);
jsonString = JsonConvert.SerializeObject(obj);

SerializeObject(obj, Formatting.None) method accepts Formatting enum as a second parameter. You can always choose if you want Formatting.Indented or Formatting.None.



来源:https://stackoverflow.com/questions/50633512/minify-a-json-string-using-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!