c# . net反序列化$type不起作用
本文关键字:type 不起作用 反序列化 net | 更新日期: 2023-09-27 18:08:15
我试图使用他们$type属性反序列化json。但是,我得到一个错误,指出"在JSON中指定的类型未解决"。我不确定我做错了什么。非常感谢任何帮助。
我的JSON
{
movies:
[
{
$type:"RTMoviePageWithSlides",
title:"Reservoir Dogs",
slides:[
{$type:"RTCharacterPage", title:"Mr. Orange", img:""},
{$type:"RTCharacterPage", title:"Mr. Blonde", img:""},
{$type:"RTCharacterPage", title:"Mr. White", img:""},
{$type:"RTCharacterPage", title:"Mr. Pink", img:""},
{$type:"RTCharacterPage", title:"Nice Guy Eddie", img:""},
]
}
{
$type:"RTMoviePageWithSubpages",
title:"Jackie Brown",
pages:[
{$type:"RTActorPage", title:"Pam Gier", other_movies:[]},
{$type:"RTActorPage", title:"Samuel L. Jackson", other_movies:[]},
{$type:"RTActorPage", title:"Robert Forester", other_movies:[]}
]
}
]
}
我的类using System.Collections;
using System.Collections.Generic;
public class RTMovieData {
public List<RTMoviePage> movies;
}
public class RTMoviePage {
public string title;
}
public class RTMoviePageWithSlides : RTMoviePage{
public List<RTMoviePage> slides = new List<RTSlidePageData>{};
}
public class RTMoviePageWithSubpages : RTMoviePage{
public List<RTMoviePage> pages = new List<RTPageData>{};
}
public class RTCharacterPage : RTMoviePage
{
public string img;
}
public class RTActorPage : RTMoviePage
{
public List<string>other_movies;
}
反序列化代码var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Auto;
RTMovieData data = JsonConvert.DeserializeObject<RTMovieData>(jsonString, settings);
@Programmer提到了"在Unity c#中使用MiniJSON反序列化JSON"这篇文章,作为这篇文章的副本。然而,这两个职位根本没有联系。另一篇文章描述了一个用户,他正在使用不同的JSON反序列化器,并且在获取数组反序列化时遇到了问题。我的问题是具体使用$type属性来帮助反序列化器实例化子类,这在这篇文章的标题中有说明。
解决方案:在我的JSON中,我需要添加"Assembly-CSharp"到$type属性的值
$type:"RTMoviePageWithSlides, Assembly-CSharp"
和
$type:"RTCharacterPage, Assembly-CSharp"