使用未知字段反序列化 JSON

发布于 2024-11-26 11:28:31 字数 1158 浏览 2 评论 0原文

我正在尝试反序列化从外部源收到的一些 JSON(无法更改它),但遇到了一些问题。我正在使用 JSON.net 进行反序列化,这是我收到的示例:

{
    "uploaded":
    {
        "name":"Uploaded by me",
        "size":3768,
        "last_change_time":1310470698
    },
    "tagged":
    {
        "name":"Photos I'm tagged in",
        "size":6937,
        "last_change_time":1311730303
    },
    "4019677_60607060":
    {
        "name":"Asad",
        "size":63,
        "last_change_time":1271315304
    },
    "4611824_60607060":
    {
        "name":"ASDF",
        "size":64,
        "last_change_time":1262645480
    }
}

通过这种方式,我创建了一个对象 Albums 和一个对象 Albums

[DataContract]
public class Albums
{
    [DataMember]
    public Album uploaded  { get; set; }
    [DataMember]
    public Album tagged { get; set; }
}

[DataContract]
public class Album
{
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public int size { get; set; }
    [DataMember]
    public int last_change_time { get; set; }
}

正如你所看到的,对于 Albums 对象我遇到了问题,因为我无法知道相册的 id 是什么,因此我无法使用该 id 在相册中创建字段。上传并标记的字段 atre 我认为 JSON.net 中应该有一个选项来实现此目的,但我找不到它......

谢谢大家

I'm trying to deserialize some JSON I receive from an external source (no way to change it) and I'm having some problems with it. I'm using JSON.net to deserialize, and this is an example of what I receive:

{
    "uploaded":
    {
        "name":"Uploaded by me",
        "size":3768,
        "last_change_time":1310470698
    },
    "tagged":
    {
        "name":"Photos I'm tagged in",
        "size":6937,
        "last_change_time":1311730303
    },
    "4019677_60607060":
    {
        "name":"Asad",
        "size":63,
        "last_change_time":1271315304
    },
    "4611824_60607060":
    {
        "name":"ASDF",
        "size":64,
        "last_change_time":1262645480
    }
}

This way, I create an object Albums and an object Album

[DataContract]
public class Albums
{
    [DataMember]
    public Album uploaded  { get; set; }
    [DataMember]
    public Album tagged { get; set; }
}

[DataContract]
public class Album
{
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public int size { get; set; }
    [DataMember]
    public int last_change_time { get; set; }
}

And as you can see, with the Albums object I've got a problem, because there's no way I know what would be the album's id, and therefore, no way I create a field in Albums with that id. Uploaded and tagged fields atre I think there should be an option in JSON.net to achieve this but I can't find it...

Thank you all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-12-03 11:28:31

收到的 JSON 似乎是一个字典..

您是否尝试将其反序列化为 Dictionnary

the JSON recieved seems to be a dictionnary..

did you try to deseserialize as a Dictionnary<string,Album> ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文