Eric S
Eric S

Reputation: 1442

C# Add Extra json Tags

I am trying to return json data from a Web Service. I am able to return data just fine but the consumer of the web service wants the data in a certain format with additional tags.

How do I add these extra tags to a json return in a C# RESTful service?

I want to add:

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

also add:

"RegisteredOwner": [

Current Return:

[
  {
    "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"
      }
    ]
  }
]

Needed Retrun:

{
  "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"
              }
            ]
          }
        }
      ]
    }
  }
}

My Code:

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);
    }
}

Method in 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; }
}

Upvotes: 0

Views: 1044

Answers (2)

starlight54
starlight54

Reputation: 1091

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
}

This should give you what you want, I've used JsonProperty assuming your using Newtonsoft.Json but you can just name the Properties with the JSON name directly if not.

Of course you need to construct and then return a ProfileResponseWrapper to be converted to JSON when fulfilling the request.

Upvotes: 2

Piet Vredeveld
Piet Vredeveld

Reputation: 135

You need to create classes for getProfilesByImisidResponse, getProfilesByImisidResult, profileResponse and RegisteredOwners

For example:

public class RegisteredOwners : List<RegisteredOwner>
{
}

Since you already have a profileResponse class i suggest to create a profileResponses class

public class profileResponses : List<profileResponse>
{
}

Upvotes: 1

Related Questions