使用Json.Net从JSON动态删除字段

使用Json.Net从JSON动态删除字段,第1张

使用Json.Net从JSON动态删除字段

您可以将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"    }  ]}

小提琴:https :
//dotnetfiddle.net/KcSuDt



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642579.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存