Я пытаюсь вернуть данные 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; }
}
обертывание профиляОтзывы в новом классе – Steve
@Steve Благодарим за предложение. –