C#添加额外的json标签

时间:2016-08-12 20:25:51

标签: c# asp.net json rest

我正在尝试从Web服务返回json数据。我能够很好地返回数据但是Web服务的使用者想要使用其他标签的特定格式的数据。

如何在C#RESTful服务中将这些额外标签添加到json返回?

我想补充一下:

  "getProfilesByImisidResponse": {
    "getProfilesByImisidResult": {
      "profileResponse": [

还添加:

"RegisteredOwner": [

当前回报:

[
  {
    "AVRRProfileId": "AVRRP000000169",
    "ESBTransactionGuId": "d28cb710-9ff5-45f8-a5a6-e779aaf07151",
    "ErrorMessage": null,
    "Transaction": null,
    "RegisteredOwners": [
      {
        "FirstName": "Kevin",
        "LastName": " Dunn"
      },
      {
        "FirstName": "Elaine",
        "LastName": " Dunn"
      }
    ]
  },
  {
    "AVRRProfileId": "AVRRP000000170",
    "ESBTransactionGuId": "d28cb710-9ff5-45f8-a5a6-e779aaf07151",
    "ErrorMessage": null,
    "Transaction": null,
    "RegisteredOwners": [
      {
        "FirstName": "Kevin",
        "LastName": " Dunn"
      },
      {
        "FirstName": "Elaine",
        "LastName": " Dunn"
      }
    ]
  }
]

需要重新启动:

{
  "getProfilesByImisidResponse": {
    "getProfilesByImisidResult": {
      "profileResponse": [
        {
          "AVRRProfileId": "AVRRP000000169",
          "ESBTransactionGuid": null,
          "ErrorMessages": null,
          "Transaction": null,
          "RegisteredOwners": {
            "RegisteredOwner": [
              {
                "FirstName": "Kevin",
                "LastName": " Dunn"
              },
              {
                "FirstName": "Elaine",
                "LastName": " Dunn"
              }
            ]
          }
        },
        {
          "AVRRProfileId": "AVRRP000000170",
          "ESBTransactionGuid": null,
          "ErrorMessages": null,
          "Transaction": null,
          "RegisteredOwners": {
            "RegisteredOwner": [
              {
                "FirstName": "Kevin",
                "LastName": " Dunn"
              },
              {
                "FirstName": "Elaine",
                "LastName": " Dunn"
              }
            ]
          }
        }
      ]
    }
  }
}

我的代码:

AVRRService.svc.cs:

using System;
...

namespace AXWCFLINQ
{
    public class AVRRService : IAVRRService
    {
        private daoAVRR daoAVRR = new daoAVRR();

        public List<profileResponse> getProfilesByImisid(IMISIdRequest imisIdRequest)
        {
            return daoAVRR.getProfilesByImisid(imisIdRequest);
        }
    }
}

IAVRRService.cs:

using System;
...

namespace AXWCFLINQ
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IAVRRService" in both code and config file together.
        [OperationContract]
        [WebInvoke
           (UriTemplate = "/getProfilesByImisid",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json, Method = "POST")] 
        List<profileResponse> getProfilesByImisid(IMISIdRequest imisIdRequest);
    }
}

daoAVRR.cs中的方法

public List<profileResponse> getProfilesByImisid(IMISIdRequest imisIdRequest)
{
    List<profileResponse> AVRRList = null;
    string IMISId = "";

    IMISId = imisIdRequest.imisId;
    try
    {
        var aVRRInfo = from a in db.AMA_AVRR_PROFILEs
                       where (a.IMISID == IMISId && a.ACTIVE == 1)
                       select new profileResponse
                       {
                           AVRRProfileId = a.AVRRPROFILEID,
                           ESBTransactionGuId = a.ESBTRANSACTIONGUID,
                           ImisId = a.IMISID,
                           RegisteredOwners = GetRegisteredOwnerList(a.REGISTEREDOWNER1, a.REGISTEREDOWNER2),
                           ErrorMessage = "",
                           Transaction = GetTransactionByAVRRProfileId(a.AVRRPROFILEID)
                       };

        AVRRList = aVRRInfo.ToList();
    }
    catch (Exception e)
    {
        string ex = e.ToString();
    }
    return AVRRList;
}

profileResponse.cs:

public class profileResponse
{
    public string AVRRProfileId { get; set; }

    public string ESBTransactionGuId { get; set; }
    public string ErrorMessage { get; set; }
    public List<RegisteredOwner> RegisteredOwners { get; set; }
    public Transaction Transaction { get; set; }
}

2 个答案:

答案 0 :(得分:2)

VideoCapture video();

这可以为您提供您想要的内容,我已经使用public class ProfileResponse { public string AVRRProfileId { get; set; } public string ESBTransactionGuId { get; set; } public string ErrorMessage { get; set; } public RegisteredOwners RegisteredOwners { get; set; } public Transaction Transaction { get; set; } } public class ProfileResponseWrapper { [JsonProperty(Name = "getProfilesByImisidResponse")] public ProfilesByImisResponse response; } public class ProfilesByImisResponse { [JsonProperty(Name = "getProfilesByImisidResult")] public ProfilesByImisResult result; } public class ProfilesByImisResult { [JsonProperty(Name = "profileResponse")] public List<ProfileResponse> ProfileResponses; } public class RegisteredOwners { public List<RegisteredOwner> RegisteredOwner; //You should consider naming these differently as this isn't ideal for clarity } 假设您使用JsonProperty但是如果没有,您可以直接使用JSON名称命名属性。

当然,您需要构造然后在完成请求时返回Newtonsoft.Json以转换为JSON。

答案 1 :(得分:1)

您需要为getProfilesByImisidResponse,getProfilesByImisidResult,profileResponse和RegisteredOwners

创建类

例如:

-L<path>

由于您已经有一个profileResponse类,我建议创建一个profileResponses类

public class RegisteredOwners : List<RegisteredOwner>
{
}