2016-08-12 7 views
0

Я пытаюсь вернуть данные json из веб-службы. Я могу вернуть данные просто отлично, но потребитель веб-службы хочет получить данные в определенном формате с дополнительными тегами.C# Добавить дополнительные json Теги

Как добавить эти дополнительные теги к возврату json в службе RESTful C#?

Я хочу добавить:

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

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

Мой код:

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

обертывание профиляОтзывы в новом классе – Steve

+0

@Steve Благодарим за предложение. –

ответ

2
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 предполагая ваши используя Newtonsoft.Json, но вы можете только имя свойств с Имя JSON напрямую, если нет.

Конечно, вам нужно построить, а затем вернуть ProfileResponseWrapper для преобразования в JSON при выполнении запроса.

+0

Спасибо. Я не использую Newtonsoft, но я могу назвать класс, как вы предлагали. –

1

Вам нужно создать классы для getProfilesByImisidResponse, getProfilesByImisidResult, profileResponse и RegisteredOwners

Например:

public class RegisteredOwners : List<RegisteredOwner> 
{ 
} 

Sinc е у вас уже есть класс profileResponse я предлагаю создать класс profileResponses

public class profileResponses : List<profileResponse> 
{ 
} 
+0

Благодарим вас за предложение. –

Смежные вопросы