简体   繁体   English

将 json 响应转换为 .net 中的动态特定 object C#

[英]convert json response to dynamic specific object in .net C#

I'm working on .NET Core where I'm trying to masking some sensitive data like emailid , password , etc.. I want all my json response data keys or properties name should be in lowercase for that I'm using one of the feature of Newtonsoft eg ContractResolver which helps us to customize property name.我在 .NET Core 上工作,我试图屏蔽一些敏感数据,如emailidpassword等。我希望我所有的 json 响应数据键或属性名称应该是小写的,因为我正在使用其中一个功能Newtonsoft 的 ContractResolver,它可以帮助我们自定义属性名称。 But it seems, it only works with generic class type eg但它似乎只适用于通用 class 类型,例如

class customer
{
   public string firstName { get; set; } 
}

but it not working when we have json string eg但是当我们有 json 字符串时它不起作用,例如

{\n   \n    \"firstName\": \"Testbusiness\",\n    \"lastName\": \"business\",\n}

now i'm looking for how to convert json response to specific object type dynamically so that i can pass that object to contractResolver checkout below code.现在我正在寻找如何将 json 响应动态转换为特定的 object 类型,以便我可以将 object 传递给代码下方的 contractResolver 结帐。

public class LowerCaseResolver : DefaultContractResolver
{
   protected override string ResolvePropertyName(string propertyName)
    {
        return propertyName.ToLower();
    }
}
using Newtonsoft.Json;
using SerializationContractResolverExample.ContractResolver;
using System;

namespace SerializationContractResolverExample
{
    class Program
    {
        static void Main(string[] args)
        {
            CustomerInfo customerInfo = new CustomerInfo()
            {
                FirstName = "Sukhpinder",
                LastName = "Singh",
                MobileNumbers = new System.Collections.Generic.List<string>() {
                 "33443434343"
                }
            };

            var sestting = new JsonSerializerSettings
            {
                ContractResolver = new LowerCaseResolver()
            };

            // need to convert below response to dynamic generic type 
            string tem1 = "{\n    \"lmsUserId\": 10268,\n    \"landlordProfileId\": \"2ea81674-6ca6-478c-a9c6-fefbe9572f28\",\n    \"firstName\": \"Testbusiness\",\n    \"lastName\": \"business\",\n    \"email\": \"yesteluydi@vusra.com\",\n    \"createdBy\": 1551,\n    \"createdDate\": \"2022-05-05T17:05:10.37\",\n    \"user\": null,\n    \"linkedLLCs\": null,\n    \"ssn\": null,\n    \"accountTypeId\": 2,\n    \"completeLater\": false\n}";

            var responseLowerCase = JsonConvert.SerializeObject(tem1, Formatting.Indented, sestting);  //Not working
            
            var responseLowerCase = JsonConvert.SerializeObject(customerInfo, Formatting.Indented, sestting); //Working Fine 

            Console.WriteLine(responseLowerCase);

            Console.ReadLine();
        }
    }
}

You can try to use JsonConvert.DeserializeObject to convert tem1 to Object .您可以尝试使用JsonConvert.DeserializeObject将 tem1 转换为Object

string tem1 = "{\n    \"lmsUserId\": 10268,\n    \"landlordProfileId\": \"2ea81674-6ca6-478c-a9c6-fefbe9572f28\",\n    \"firstName\": \"Testbusiness\",\n    \"lastName\": \"business\",\n    \"email\": \"yesteluydi@vusra.com\",\n    \"createdBy\": 1551,\n    \"createdDate\": \"2022-05-05T17:05:10.37\",\n    \"user\": null,\n    \"linkedLLCs\": null,\n    \"ssn\": null,\n    \"accountTypeId\": 2,\n    \"completeLater\": false\n}";
   var s = JsonConvert.DeserializeObject<Object>(tem1);

result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM