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
我正在尝试使用一些由不同团队处理的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
非常简单,也不痛苦。
相关文章:
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 在调用speak之前预加载Web Speech API
- 如何在visualstudio中调试web api时编辑javascript文件
- 将复杂对象从angular js传递到web api,它总是返回404
- 基于api密钥的NodeJS web服务
- 将文档上载到Web API
- AngularJS:使用维基百科API的web服务
- IndexedDB同步api+web工作者-what'这就是重点
- javascript函数包含两个元素和web音频api
- 如何更改web应用程序的语言.有没有这样的api
- 在Web应用程序中绑定API和模板路由的位置
- Canvas API和Web工作者
- 对/Token的ASP Web API调用间歇性
- 从其他域和计算机发布的 Web API 帖子
- Web API 调用在设备模式下失败
- Testing .NET Web API from JavaScript
- 将日期作为从 Web API 到 angularJS 的 json 响应
- 获取 Spotify 用户 [Web API] 当前播放的曲目名称
- 如何在Javascript和PHP中设置带有URL身份验证的API web密钥
- 调用javascript api,但在仪表板上显示为Places api Web服务