JSON中的Web Api数据被包装在一个对象中,为什么会这样,我如何告诉Web Api所有者该怎么做来解决这个问题

Web Api Data in JSON is wrapped in an object , why is this and how can I tell the Web Api owner what to do to fix this

本文关键字:Api Web 何告诉 所有者 问题 解决 包装 数据 一个对象 JSON 为什么      更新时间:2023-09-26

我正在尝试使用一些由不同团队处理的Web Api数据,他们似乎并不认为数据有任何问题。

虽然我意识到这是有效的JSON,但我想告诉他们我希望他们发送什么,以便在我的客户端没有更多的工作要做。

Postman和Angular(任何东西)最终会把数据封装在这个"Devices"

{
  "Devices": [
    {
      "DeviceId": "00022B9A000000010001",
      "StagedManifestIdList": [],
      "PendingManifestId": null,
      "PendingTimeStamp": "0001-01-01T00:00:00",
      "ManifestIdList": [
        "00000002",
        "00000001",
        "00000003"
      ],
      "DeviceStatus": 3,
      "Aid": "oAAABTUAAg==",
      "DKiIndex": "DKi00000002",
      "Sha": "R2tiZRQgY/iohXZt5O4HaQwtVe/adWU2VOcKaelJ3Us=",
      "DefaultPayload": "C:''ProgramData''ABC''124''Payloads''M4PayloadAuto.xml"
    }
  ]
}

所以上面的Devices对我来说是很痛苦的。

他们确实给我发送了他们的Web Api方法的代码,他们能改变什么,所以我没有得到这个"设备"包装器名称?

public class DeviceController : ApiController
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof (DeviceController).Name);
        [ResponseType(typeof (RetrieveDeviceResponse))]
        public IHttpActionResult Get(string id = null)
        {
            IHttpActionResult httpActionResult;
            var returnVal = new RetrieveDeviceResponse();
            try
            {
                byte[] deviceIdBytes = null;
                if (!string.IsNullOrWhiteSpace(id))
                {
                    deviceIdBytes = ByteArray.ConvertHexStringToByteArray(id);
                }
                var deviceInfoArray = DeviceManager.RetrieveDevice(deviceIdBytes);
                if (deviceInfoArray == null)
                {
                    returnVal.Success = false;
                    returnVal.ErrorMessage = "Get: RetrieveDevice returned null";
                }
                else
                {
                    returnVal.Devices = new List<DeviceInfo>();
                    foreach (var deviceInfoObj in deviceInfoArray)
                    {
                        DeviceInfo deviceInfo = new DeviceInfo
                        {
                            Aid = deviceInfoObj.AID,
                            DKiIndex = deviceInfoObj.DKiIndex,
                            DefaultPayload = deviceInfoObj.DefaultPayload,
                            DeviceId = ByteArray.ConvertToHexString(deviceInfoObj.DevID),
                            StagedManifestIdList = deviceInfoObj.StagedManifestIDList,
                            PendingManifestId = deviceInfoObj.PendingManifestID,
                            PendingTimeStamp = deviceInfoObj.PendingTimeStamp,
                            ManifestIdList = deviceInfoObj.ManifestIDList,
                            DeviceStatus = (DeviceStatus) deviceInfoObj.DeviceStatus,
                            Sha = deviceInfoObj.SHA
                        };
                        returnVal.Devices.Add(deviceInfo);
                    }
                    returnVal.Success = true;
                }
            }
            catch (Exception ex)
            {
                returnVal.ErrorMessage = "Get: Exception. " + DebugMode.ShowMessage(ex.Message);
                returnVal.Success = false;
                Log.Error(returnVal.LastErrorMessage);
            }

            if (returnVal.Success)
            {
                httpActionResult = Ok(returnVal);
                Log.Debug("Get: Success");
            }
            else
            {
                httpActionResult = Content(HttpStatusCode.BadRequest, returnVal);
                Log.Error("Get Failed: " + returnVal.ErrorMessage);
            }
            return httpActionResult;
        }
    }

你能确切地说一下为什么会痛吗?正如我所看到的,它们不仅返回有关设备的信息,还返回异常和成功状态。例如,如果出现错误,您将收到如下内容:

{
  "Devices": [], 
  "ErrorMessage" : "Some Error Message",
  "Success" : false
}

忽略这一点可能会导致一些问题。

但是,如果您坚持只需要Devices的列表而不需要其他列表,请告诉他们返回的不是包裹在HttpActionResult中的returnVal,而是returnVal.Devices。例如,修改

  if (returnVal.Success)
  {
       httpActionResult = Ok(returnVal);
       Log.Debug("Get: Success");
  }

:

  if (returnVal.Success)
  {
       httpActionResult = Ok(returnVal.Devices);
       Log.Debug("Get: Success");
  }

然而,我坚持你使用他们提供的。从客户端提供的对象获取Devices非常简单,也不痛苦。