咨询区

  • mpen

我在寻找一个 function,它能够接收 string 格式的json,并且能够以 换行+缩进 的形式输出,比如:


{"status":"OK", "results":[ {"types":[ "locality", "political"], "formatted_address":"New York, NY, USA", "address_components":[ {"long_name":"New York", "short_name":"New York", "types":[ "locality", "political"]}, {"long_name":"New York", "short_name":"New York", "types":[ "administrative_area_level_2", "political"]}, {"long_name":"New York", "short_name":"NY", "types":[ "administrative_area_level_1", "political"]}, {"long_name":"United States", "short_name":"US", "types":[ "country", "political"]}], "geometry":{"location":{"lat":40.7143528, "lng":-74.0059731}, "location_type":"APPROXIMATE", "viewport":{"southwest":{"lat":40.5788964, "lng":-74.2620919}, "northeast":{"lat":40.8495342, "lng":-73.7498543}}, "bounds":{"southwest":{"lat":40.4773990, "lng":-74.2590900}, "northeast":{"lat":40.9175770, "lng":-73.7002720}}}}]} 

格式化后:


{"status":"OK","results":[{"types":["locality","political"],"formatted_address":"New York, NY, USA","address_components":[{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"New York","short_name":"New York","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"geometry":{"location":{"lat":40.7143528,"lng":-74.0059731},"location_type":"APPROXIMATE","viewport":{"southwest":{"lat":40.5788964,"lng":-74.2620919},"northeast":{"lat":40.8495342,"lng":-73.7498543}},"bounds":{"southwest":{"lat":"40.4773990","lng":"-74.2590900"},"northeast":{"lat":"40.9175770","lng":"-73.7002720"}}}}]
}

请问有谁知道 C# 中是否有这样的 library 吗?

回答区

  • Gurdeep Singh Sidhu

.NET Core 3 新增了一个 System.Text.Json 类可以帮我实现这样的功能。

public string PrettyJson(string unPrettyJson){var options = new JsonSerializerOptions(){WriteIndented = true};var jsonElement = JsonSerializer.Deserialize<JsonElement>(unPrettyJson);return JsonSerializer.Serialize(jsonElement, options);}
  • Frank Tzanabetis

你可以使用 Newtonsoft.Json 来实现,在 SerializeObject 的时候带一个枚举参数,参考如下代码:


var x = JsonConvert.SerializeObject(jsonString, Formatting.Indented);

或者用它的 JToken 来实现。


void Main()
{//Example 1var t = "{\"x\":57,\"y\":57.0,\"z\":\"Yes\"}";var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(t); var f = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);Console.WriteLine(f);//Example 2JToken jt = JToken.Parse(t);string formatted = jt.ToString(Newtonsoft.Json.Formatting.Indented);Console.WriteLine(formatted);//Example 2 in one line -Console.WriteLine(JToken.Parse(t).ToString(Newtonsoft.Json.Formatting.Indented));
}

点评区

对json进行格式化输出,确实是一个非常好的功能,在以前我可能还需要copy到 json.cn 格式化一下,要知道 json.cn 是要上传数据的,有一定的潜在风险,现在方便多了!

NET问答: C# 中是否有 format json 的类库?相关推荐

  1. python -m json.tool 中文乱码 Format JSON with python

    python -m json.tool 中文乱码 Format JSON with python 现在以 json 为数据传输格式的 RESTful 接口非常流行.为调试这样的接口,一个常用的办法是使 ...

  2. SQL Server 2016中的本机JSON支持

    With the introduction of SQL Server 2016, Microsoft introduced many new features which had taken SQL ...

  3. 分层条件关系网络在视频问答VideoQA中的应用:CVPR2020论文解析

    分层条件关系网络在视频问答VideoQA中的应用:CVPR2020论文解析 Hierarchical Conditional Relation Networks for Video Question ...

  4. .NET Core类库项目中如何读取appsettings.json中的配置

    这是一位朋友问我的问题,写篇随笔回答一下.有2种方法,一种叫丑陋的方法 -- IConfiguration ,一种叫优雅的方法 -- IOptions . 1)先看丑陋的方法 比如在 RedisCli ...

  5. android post json格式,Android中post请求传递json数据给服务端的实例

    在最近的项目中有个需求是这样的: 入参封装成JSON,EXAMPLE: { "uuid": "iamauuid","clientType": ...

  6. 在Flutter中解析复杂的JSON(一篇顶十篇)

    文章目录 JSON结构#1:简单 map 访问对象 Snippet #1 : imports **Snippet #2 : **加载Json Asset(可选)** Snippet #3 : 加载响应 ...

  7. cJSON_译(C中的超轻量级JSON解析器)

    cJSON ANSI C中的超轻量级JSON解析器. 通行证 执照 版权所有(c)2009-2017 Dave Gamble和cJSON贡献者 特此免费授予获得此软件和相关文档文件("软件& ...

  8. python类定义中、对象字符串的特殊方法是_python中自定义类对象json字符串化的方法_python json转字符串、...

    python中自定义类对象json字符串化的方法 1. 用 json 或者simplejson 就可以 2.定义转换函数: def convert_to_builtin_type(obj): prin ...

  9. time包中Parse和Format的区别

    time包中Parse和Format的区别 参考代码 package mainimport("fmt""time" )// 规定parse和Format的参考时 ...

最新文章

  1. eclipse安装Eclipse HTML Editor插件
  2. ubuntu 环境下调试mysql源码_【转】Ubuntu 16.04下 Mysql 5.7.17源码编译与安装
  3. Windows10——荣耀笔记本任务栏图标显示异常且无显示/隐藏图标的箭头解决方案
  4. 零基础Python知识点回顾(一)
  5. 数据库设计-规范化规则
  6. apollo 配置中心_Spring Cloud 系列之 Apollo 配置中心(三)
  7. 手机展示海报PSD模板、适用众多设计!
  8. centos7 挂载磁盘_Linux磁盘管理之LVM
  9. pg数据库中postgis的使用
  10. 日志追踪-Java字节码-类文件结构
  11. 国外游戏开发公司10强
  12. dnf服务器运行库,rpg游戏运行库运行环境软件(RPGVXACE RTP)
  13. matlab计算轮廓曲率半径,matlab求曲率半径
  14. Memory cgroup out of memory
  15. 微信小程序01---小程序初始
  16. Caliburn.Micro学习笔记目录——其他
  17. CS224W-07:图神经网络二
  18. 质量管理-----解决问题A3报告写法
  19. 【转】机器学习和神经科学:你的大脑也在进行深度学习吗?
  20. How to modify comment and attribute ClearCase

热门文章

  1. IT学习--学习不一定要用在现实工作中
  2. .net开发是做什么的_软件开发是什么, 该怎么做?
  3. javascript 手机手势动作touch触屏原理分析
  4. kaggle入门项目:Titanic存亡预测(三)数据可视化与统计分析
  5. html标记语言 --框架
  6. 指针04 - 零基础入门学习C语言44
  7. 【小安翻唱】凉宫春日的忧郁--冒険でしょでしょ第五届外语歌曲大赛助兴节目~绫魂论坛送aya的生日礼物筹备开始~...
  8. Microsoft Teams:团队Owner离开公司后,我们该怎么做?
  9. android mvvm 官方例子,详解Android的MVVM框架 - 数据绑定
  10. 程序改变了命运,程序生活一天比一天好,对未来也充满了希望