gpt4 book ai didi

c# - 使用 Json.Net 从 JSON 中动态删除字段

转载 作者:行者123 更新时间:2023-11-30 13:36:05 25 4
gpt4 key购买 nike

我有一些 JSON 输入,我无法预测其形状,我必须进行一些转换(对其进行命名)以便不记录某些字段。例如,如果我有这个 JSON:

{
"id": 5,
"name": "Peter",
"password": "some pwd"
}

然后在转换之后它应该是这样的:

{
"id": 5,
"name": "Peter"
}

上面的示例很简单,但实际情况并不那么愉快/容易。我将有一些正则表达式,如果 input JSON 上的任何字段与它匹配,那么它不应该出现在结果中。如果我有一些嵌套对象,我将不得不递归地进行。我一直在 LINQ to JSON 上看到一些东西,但没有找到满足我需求的东西。

有没有办法做到这一点?

注意:这是日志库的一部分。如有必要或更方便,我可以使用 JSON 字符串。问题是,在我的日志记录管道中的某个时刻,我得到了对象(或根据需要的字符串),然后我需要从中删除敏感数据,例如 passwords,还有任何其他客户端-指定数据。

最佳答案

您可以将 JSON 解析为 JToken,然后使用递归辅助方法将属性名称与您的正则表达式匹配。只要有匹配项,您就可以从其父对象中删除该属性。删除所有敏感信息后,只需使用 JToken.ToString() 获取编辑后的 ​​JSON。

辅助方法可能如下所示:

public static string RemoveSensitiveProperties(string json, IEnumerable<Regex> regexes)
{
JToken token = JToken.Parse(json);
RemoveSensitiveProperties(token, regexes);
return token.ToString();
}

public static void RemoveSensitiveProperties(JToken token, IEnumerable<Regex> regexes)
{
if (token.Type == JTokenType.Object)
{
foreach (JProperty prop in token.Children<JProperty>().ToList())
{
bool removed = false;
foreach (Regex regex in regexes)
{
if (regex.IsMatch(prop.Name))
{
prop.Remove();
removed = true;
break;
}
}
if (!removed)
{
RemoveSensitiveProperties(prop.Value, regexes);
}
}
}
else if (token.Type == JTokenType.Array)
{
foreach (JToken child in token.Children())
{
RemoveSensitiveProperties(child, regexes);
}
}
}

下面是一个简短的使用演示:

public static void Test()
{
string json = @"
{
""users"": [
{
""id"": 5,
""name"": ""Peter Gibbons"",
""company"": ""Initech"",
""login"": ""pgibbons"",
""password"": ""Sup3rS3cr3tP@ssw0rd!"",
""financialDetails"": {
""creditCards"": [
{
""vendor"": ""Viza"",
""cardNumber"": ""1000200030004000"",
""expDate"": ""2017-10-18"",
""securityCode"": 123,
""lastUse"": ""2016-10-15""
},
{
""vendor"": ""MasterCharge"",
""cardNumber"": ""1001200230034004"",
""expDate"": ""2018-05-21"",
""securityCode"": 789,
""lastUse"": ""2016-10-02""
}
],
""bankAccounts"": [
{
""accountType"": ""checking"",
""accountNumber"": ""12345678901"",
""financialInsitution"": ""1st Bank of USA"",
""routingNumber"": ""012345670""
}
]
},
""securityAnswers"":
[
""Constantinople"",
""Goldfinkle"",
""Poppykosh"",
],
""interests"": ""Computer security, numbers and passwords""
}
]
}";

Regex[] regexes = new Regex[]
{
new Regex("^.*password.*$", RegexOptions.IgnoreCase),
new Regex("^.*number$", RegexOptions.IgnoreCase),
new Regex("^expDate$", RegexOptions.IgnoreCase),
new Regex("^security.*$", RegexOptions.IgnoreCase),
};

string redactedJson = RemoveSensitiveProperties(json, regexes);
Console.WriteLine(redactedJson);
}

这是结果输出:

{
"users": [
{
"id": 5,
"name": "Peter Gibbons",
"company": "Initech",
"login": "pgibbons",
"financialDetails": {
"creditCards": [
{
"vendor": "Viza",
"lastUse": "2016-10-15"
},
{
"vendor": "MasterCharge",
"lastUse": "2016-10-02"
}
],
"bankAccounts": [
{
"accountType": "checking",
"financialInsitution": "1st Bank of USA"
}
]
},
"interests": "Computer security, numbers and passwords"
}
]
}

fiddle :https://dotnetfiddle.net/KcSuDt

关于c# - 使用 Json.Net 从 JSON 中动态删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116088/

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