本文介绍了使用.NET缩小json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何清理/缩小现有的json字符串?我已经看到正则表达式正在使用中.还有其他(也许更有效)的方法吗?

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

推荐答案

Install-Package Newtonsoft.Json

只需解析它,然后序列化回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)方法接受Formatting枚举作为第二个参数.您始终可以选择要Formatting.Indented还是Formatting.None.

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

这篇关于使用.NET缩小json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:58