使用未知字段反序列化JSON [英] Deserializing JSON with unknown fields

查看:56
本文介绍了使用未知字段反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化从外部来源收到的某些JSON(无法对其进行更改),并且遇到了一些问题.我正在使用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
    }
}

这样,我创建了一个对象Albums和一个对象Album

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; }
}

如您所见,使用Albums对象存在一个问题,因为我无法知道相册的ID,因此也无法在具有该ID的Albums中创建字段.我认为上载和标记的字段atre是我想在JSON.net中应该有一个选项来实现,但是我找不到它...

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...

谢谢大家

推荐答案

收到的JSON似乎是字典.

the JSON recieved seems to be a dictionnary..

您尝试过反序列化为Dictionnary<string,Album>吗?

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

这篇关于使用未知字段反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆