NET问答: C# 中是否有 format json 的类库?
生活随笔
收集整理的這篇文章主要介紹了
NET问答: C# 中是否有 format json 的类库?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
咨詢區
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 的类库?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序有并发错误?NO WAY!
- 下一篇: c# char unsigned_dll