NET问答: C# 中是否有 format json 的类库?
Posted dotNET跨平台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 1
var 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 2
JToken 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 的类库?的主要内容,如果未能解决你的问题,请参考以下文章
NET问答: 如何使用 C# 直接从 JSON String 中提取特定值 ?
NET问答:null != variable 和 variable != null 到底有什么区别?