我有以下json字符串(jsonString)
[
{
"name":"Fruits",
"references":[
{"stream":{"type":"reference","size":"original",id":"1"}},
],
"arts":[
{"stream":{"type":"art","size":"original","id":"4"}},
{"stream":{"type":"art","size":"medium","id":"9"}},
]
}
]
以及以下C#对象
class Item
{
public string Name { get; set; }
public List<Stream> References { get; set; }
public List<Stream> Arts { get; set; }
public Item()
{
}
}
class Stream
{
public string Type { get; set; }
public string Size { get; set; }
public string Id { get; set; }
public Stream()
{
}
}
和以下代码
Item item = JsonConvert.DeserializeObject<Item>(jsonString);
当我运行代码时,它会创建正确数量的引用和arts,但每个流都有null值(type = null,size = null)。
是否可以执行此json.net deserializeobject方法或者我应该手动反序列化?
答案 0 :(得分:9)
[
{
"name":"Fruits",
"references":[
{"Type":"reference","Size":"original","Id":"1"},
],
"arts":[
{"Type":"art","Size":"original","id":"4"},
{"type":"art","size":"medium","id":"9"},
]
}
]
如果您无法更改JSON,则可能需要在对象模型中引入新的包装类型:
public class StreamWrapper
{
public Stream Stream { get; set; }
}
然后让Item
班级有List<StreamWrapper>
个变量而不是List<Stream>
。这有帮助吗?