gpt4 book ai didi

c# - 在 .Net 中查找并替换 JSON 响应中的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:11 25 4
gpt4 key购买 nike

我从网络服务收到以下类型的 JSON 响应:

{"students":[{"studentID":"123456789","section":"S","RefId":"     ","pastScore":"99952","currentScore":"99952","inquiryAllowed":true},{"studentID":"223456789","section":"R","RefId":"     ,"pastScore":"152","currentScore":"952","inquiryAllowed":true},{"studentID":"323456789","section":"T","RefId":"     ,"pastScore":"4582","currentScore":"5952","inquiryAllowed":true},],"schoolName":"WatsonPrimarySchool"}

我需要屏蔽 studentID、pastScore、currentScore 和 schoolName。输出将是这样的:

{"students":[{"studentID":"$$$$$$$$","section":"S","RefId":"     ","pastScore":"$$$$$","currentScore":"$$$$$","inquiryAllowed":true},{"studentID":"$$$$$$$$$","section":"R","RefId":"     ,"pastScore":"$$$","currentScore":"$$$","inquiryAllowed":true},{"studentID":"$$$$$$$$$","section":"T","RefId":"     ,"pastScore":"$$$$","currentScore":"$$$$","inquiryAllowed":true},],"schoolName":"$$$$$$$$$$$$$$$$$$$"}

schoolName 只会出现一次,其余的可以出现多次。

我尝试使用 StringBuilder 但由于值的动态特性,它没有成功。有人可以建议 Regex 解决方案吗?

最佳答案

您可以使用 Json.Net和林克

var propsToMask = new HashSet<string>(new[] { "studentID", "pastScore", "currentScore", "schoolName" });
var jObj = JObject.Parse(json);

foreach(var p in jObj.Descendants()
.OfType<JProperty>()
.Where(p => propsToMask.Contains(p.Name)))
{
p.Value = "$$$$$$";
}

var newJsonStr = jObj.ToString();

关于c# - 在 .Net 中查找并替换 JSON 响应中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33130489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com