我在 C# 中有以下示例 json 字符串{ "AccountNumberGenerationRequest": { "BranchNumber": 8034, "AccountType": "041" }, "CreateDepositAccountRequest": { "AccountNumber": "9999999999", "BranchNumber": 8034, "AccountType": 41, "WithholdingIndicator": "4" }, "AccountNameAddressRequest": { "AccountNumber": "9999999999", "NameAndAddressType": 1, }, "CustomerAccountRelationshipRequest": { "CustomerNumber1": "58008", "Customer1ToAccountRelationshipCode": "000" }, "UpdatePartialInformationRequest": { "AccountNumber": "9999999999", "PartialInformationList": [{ "KeywordCode": "FDWTHE", "KeywordValue": "1" }] }, "RequestUUID": "557d5442-8a28-4dab-b191-fe1596ddf2b8"}并且想要读取所有具有结束值的唯一键。像下面["BranchNumber","AccountType","AccountNumber","BranchNumber","AccountType","WithholdingIndicator","AccountNumber","NameAndAddressType","CustomerNumber1","Customer1ToAccountRelationshipCode","KeywordCode","KeywordValue","RequestUUID"]我正在使用以下代码var jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; var json= JObject.Parse(JsonConvert.SerializeObject(p, jsonSerializerSettings)); IList<string> keys = json.Properties().Select(c=> c.Name).ToList(); foreach(string key in keys) { Console.WriteLine(key); }但得到只有AccountNumberGenerationRequestAccountNumberGenerationRequestAccountNameAddressRequestAccountNameAddressRequestUpdatePartialInformationRequestRequestUUID我怎样才能在 C# 中得到这个。这不是硬编码的 Json。它可以是任何 json。
1 回答
森林海
TA贡献2011条经验 获得超2个赞
您只需要展平对象并相应地过滤:
var uniqueKeys = json.Descendants()
.OfType<JProperty>()
.Where(x => x.Value is JValue)
.Select(x => x.Name)
.Distinct();
- 1 回答
- 0 关注
- 244 浏览
添加回答
举报
0/150
提交
取消