将JSON映射到C#类,其属性名称包含空格和保留字

时间:2016-07-15 08:24:32

标签: c# json rest restsharp

我正在使用REST API服务,并且在JSON的属性名称的Service输出中包含空格和保留字。当我试图将它映射到C#类时出现问题,但我无法分配与此类似的属性名称。

如果我能解决这个问题,或者有其他替代方法,请告诉我。

以下是从服务返回的JSON:

{
   "value": [   {
      "accountNo": "JKBXXXXXXXXXVNX1",
      "mainSalesPerson": "XXXXX",
      "accountName": "XXXXXX",
      "ledgerBalance": "-2,298.70",
      "T+3": "0.00",
      "T+4": "0.00",
      "T+5 and Above": "2,298.70",
      "riskRatedPortfolioValue": "109,057.50",
      "exposure": "106,758.80",
      "costToFirm": "",
      "incomeToFirm": "14.59",
      "costToIncome(%)": "",
      "exposure(%)": "2.11",
      "O/S Balance": "-2,298.70"
   }],
   "success": true
}

我使用了以下Mapping C#Class:

public class CountObject
    {
        public string value { get; set; }
        public string success { get; set; }
    }

    public class RiskManagementQueryValueObject
    {
        public string accountNo { get; set; }
        public string mainSalesPerson { get; set; }
        public string accountName { get; set; }
        public string ledgerBalance { get; set; }

        [SerializeAs(Name = "T+3")]
        public string T3 { get; set; }

        [SerializeAs(Name = "T+4")]
        public string T4 { get; set; }

        [SerializeAs(Name = "T+5 and Above")]
        public string T5_and_Above { get; set; }
        public string riskRatedPortfolioValue { get; set; }
        public string exposure { get; set; }
        public string costToFirm { get; set; }
        public string incomeToFirm { get; set; }

        [SerializeAs(Name = "costToIncome(%)")]
        public string costToIncome { get; set; }

        [SerializeAs(Name = "exposure(%)")]
        public string exposure_per { get; set; }

        [SerializeAs(Name = "O/S Balance")]
        public string OS_Balance { get; set; }
    }

    public class RiskManagementQueryHeadertObject
    {
        public List<RiskManagementQueryValueObject> value { get; set; }
        public bool success { get; set; }
    }

请注意我已经使用了上述内容,但是当JSON属性和C#类名称匹配时,映射确实发生,其余值为null

这是我如何与服务部门联系:

public List<RiskManagementQueryValueObject> getOverdues()
        {
            List<RiskManagementQueryValueObject> overdues = new List<RiskManagementQueryValueObject>();
            var count = 0.0;

            try
            {
                var restProxy = new RESTProxy();
                var request = restProxy.initRestRequest("/cam/riskmanagement/1/query/count", Method.POST, new { requestCriteriaMap = new { currency = "LKR" } });
                CountObject data = restProxy.Execute<CountObject>(request);

                if (data.success.ToString().ToLower() != "true")
                    return null;

                //get the page count
                var pageCount = Math.Truncate(count / 300) + 1;

                for (int j = 0; j < pageCount; j++)
                {
                    var request2 = restProxy.initRestRequest(string.Format("/cam/riskmanagement/1/query/{0}", (j + 1).ToString()), Method.POST, new { requestCriteriaMap = new { currency = "LKR" } });
                    RiskManagementQueryHeadertObject data2 = restProxy.Execute<RiskManagementQueryHeadertObject>(request2);

                    overdues.AddRange(data2.value);
                }
            }
            catch (Exception)
            {
                overdues = null;
            }

            return overdues;
        }
    }

    public class RESTProxy
        {
            string DEFAULT_HOST = "http://xxx.xxx.xxx.xxx:xxx/";
            string DEFAULT_MODULE = "xxx-rest";
            string DEFAULT_USER = "xxx:xxx:xxx";
            string DEFAULT_PASS = "xxx";

            public T Execute<T>(RestRequest request) where T : new()
            {
                var client = new RestClient(string.Concat(DEFAULT_HOST, DEFAULT_MODULE))
                {
                    Authenticator = new DigestAuthenticator(DEFAULT_USER, DEFAULT_PASS, DEFAULT_HOST)
                };

                var response = client.Execute<T>(request);

                if (response.ErrorException != null)
                {
                    throw response.ErrorException;
                }
                return response.Data;
            }

            public RestRequest initRestRequest(string pRestResourceName, Method pRestServiceVerb, object pRestRequestBody)
            {
                var request = new RestRequest(pRestResourceName, pRestServiceVerb);
                request.RequestFormat = DataFormat.Json;


                if (pRestRequestBody != null)
                {
                    request.AddBody(pRestRequestBody);
                }

                return request;
            }
        }

我该如何解决这个问题?

更新

我解决了这个问题。我添加了[DeserializeAs(Name ="")]而不是[SerializeAs(Name ="")]我只是在我的C#Map类中执行了此操作:

public class RiskManagementQueryValueObject
    {
        public string accountNo { get; set; }
        public string mainSalesPerson { get; set; }
        public string accountName { get; set; }
        public string ledgerBalance { get; set; }

        [DeserializeAs(Name = "T+3")]
        public string T3 { get; set; }

        [DeserializeAs(Name = "T+4")]
        public string T4 { get; set; }

        [DeserializeAs(Name = "T+5 and Above")]
        public string T5_and_Above { get; set; }
        public string riskRatedPortfolioValue { get; set; }
        public string exposure { get; set; }
        public string costToFirm { get; set; }
        public string incomeToFirm { get; set; }

        [DeserializeAs(Name = "costToIncome(%)")]
        public string costToIncome { get; set; }

        [DeserializeAs(Name = "exposure(%)")]
        public string exposure_per { get; set; }

        [DeserializeAs(Name = "O/S Balance")]
        public string OS_Balance { get; set; }
    }

2 个答案:

答案 0 :(得分:6)

使用JsonProperty属性对我有用:

示例:

这个C#类:

public class Product
{
    [JsonProperty(PropertyName = "Name/ + 1")]
    public string Name { get; set; }
}

映射到以下javascript对象:

var input = {"Name/ + 1": "PostName"};

答案 1 :(得分:1)

我解决了这个问题。我添加了[DeserializeAs(Name ="")]而不是[SerializeAs(Name ="")]。我只是在我的C#Map类中完成了这个:

public class RiskManagementQueryValueObject
    {
        public string accountNo { get; set; }
        public string mainSalesPerson { get; set; }
        public string accountName { get; set; }
        public string ledgerBalance { get; set; }

        [DeserializeAs(Name = "T+3")]
        public string T3 { get; set; }

        [DeserializeAs(Name = "T+4")]
        public string T4 { get; set; }

        [DeserializeAs(Name = "T+5 and Above")]
        public string T5_and_Above { get; set; }
        public string riskRatedPortfolioValue { get; set; }
        public string exposure { get; set; }
        public string costToFirm { get; set; }
        public string incomeToFirm { get; set; }

        [DeserializeAs(Name = "costToIncome(%)")]
        public string costToIncome { get; set; }

        [DeserializeAs(Name = "exposure(%)")]
        public string exposure_per { get; set; }

        [DeserializeAs(Name = "O/S Balance")]
        public string OS_Balance { get; set; }
    }