问题描述
我正在尝试反序列化来自 API 的 JSON 响应.JSON 如下所示(MAC 地址和位置已更改):
I'm trying to deserialize a JSON response from an API. The JSON looks like this (MAC addresses and location are altered):
问题在于措施块.由于对象的名称正在更改,我不知道如何将其正确反序列化为 C# 对象.我在这里发现了一个与使用字典的解决方案类似的问题,但是如果我这样尝试,我只会得到 null目录.
The problem is the measures block. Since the name of the object is changing, I don't know how to deserialize it properly into a C# object. I found a similiar problem on here with the solution to use a dictonary, however if I try it this way I just get null directory.
这是我的反序列化方法:
This is my deserialize method:
这是 APIResponse 类:
And this is the APIResponse class:
有什么方法可以轻松地正确反序列化这些措施?
Any way to properly derserialize the measures easily?
推荐答案
看起来你应该能够摆脱你的 Measures
类.相反,将字典直接放入您的 Body
类:
It looks like you should be able to just get rid of your Measures
class. Instead, put the dictionary straight into your Body
class:
另外,我强烈建议您的属性遵循 .NET 命名约定,并使用 [JsonProperty("measures")]
等向 Json.NET 指示如何翻译您的属性属性转换成 JSON.
As a separate matter, I'd strongly recommend following .NET naming conventions for your properties, and using [JsonProperty("measures")]
etc to indicate to Json.NET how to translate your properties into JSON.
这篇关于使用未知对象名称反序列化 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!