使用未知字段反序列化 JSON
我正在尝试反序列化从外部源收到的一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
收到的 JSON 似乎是一个字典..
您是否尝试将其反序列化为
Dictionnary
?the JSON recieved seems to be a dictionnary..
did you try to deseserialize as a
Dictionnary<string,Album>
?