如何正确序列化和反序列化Json中的对象列表? - c#

我有这样的BaseView

public abstract class BaseView 
{
    public enum ViewType
    {
        NOT_DEFINED,
        CHECK_BOX
    }

    virtual public string Title { get; } = string.Empty;
    virtual public string Name { get; } = string.Empty;
    virtual public string ConfigName { get; } = string.Empty;
    virtual public int DefaultValue { get; } = MCConstants.DEFAULT_VAL;
    virtual public ViewType Type { get; } = ViewType.NOT_DEFINED;

    public BaseView(string title, string configName, int defaultValue, ViewType viewType)
    {
        Title = title;
        Name =  title.Replace(' ', '_');
        ConfigName = configName;
        DefaultValue = defaultValue;
        Type = viewType;
    }
}

和他的孩子

public class DynamicCheckBox : BaseView
{
    public bool IsChecked { get; set; }

    public DynamicCheckBox(string title, string configName, int defaultValue) : base(title, configName, defaultValue, ViewType.CHECK_BOX)
    {
        IsChecked = DefaultValue == MCConstants.TRUE ? true : false;
    }
}

现在,我需要序列化DynamicCheckBox的列表,然后反序列化它。为此,我写了这样的方法

public static string ParseObjListToString<TYPE>(IList<TYPE> list)
{
    return JsonConvert.SerializeObject(new { list });         <--- This line
}

public static IList<TYPE> ParseStringToListOfObj<TYPE>(string value)
{
    return JsonConvert.DeserializeObject<IList<TYPE>>(value);
}

我得到这样的错误

Newtonsoft.Json.JsonSerializationException:'无法反序列化当前JSON对象
(例如{“ name”:“ value”})类型为'System.Collections.Generic.IList`1 [TV_MeshCreatorEngine.Base.BaseView]'
因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
要解决此错误,请将JSON更改为JSON数组(例如[1,2,3])
或更改反序列化类型,使其成为普通的.NET类型
(例如,不是整数之类的原始类型,不是数组或List之类的集合类型)
可以从JSON对象反序列化。也可以将JsonObjectAttribute添加到
类型以强制其从JSON对象反序列化。
路径“列表”,第1行,位置8。”

有我的测试方法

private void TestMethod()
{
    IList<BaseView> list = new List<BaseView>()
    {
        new DynamicCheckBox("titleOne", "configNameOne", 1)
    };

    var stringResult = JsonUtil.ParseObjListToString(list);
    IList<BaseView> listResult = JsonUtil.ParseStringToListOfObj<BaseView>(stringResult);
}

我究竟做错了什么?

编辑

如果我改变这种方法

public static string ParseObjListToString<TYPE>(IList<TYPE> list)
{
    return JsonConvert.SerializeObject(new { list });         <--- This line
}

对此

public static string ParseObjListToString<TYPE>(IList<TYPE> list)
{
    return JsonConvert.SerializeObject(list );         <--- This line
}

反正我得到错误

Newtonsoft.Json.JsonSerializationException:'无法创建
TV_MeshCreatorEngine.Base.BaseView类型的实例。类型是
接口或抽象类,无法实例化。路径
'[0] .IsChecked',第1行,位置14。'

参考方案

您正在使用名为IEnumerable 的名为list的字段序列化对象,而不是序列化为array。

更改

return JsonConvert.SerializeObject(new { list });

return JsonConvert.SerializeObject(list);

编辑:
因为您的输出JSON看起来像:
{ list: [] }

Newtonsoft.Json无法将其反序列化为数组[]

您还可以更改反序列化以匹配序列化的对象。

为什么要使用Func <string>而不是string? - c#

为什么要使用Func<string>而不是string?我的问题特别是关于this回购。有问题的行是22: private static Func<string> getToken = () => Environment.GetEnvironmentVariable("GitHubToken", Enviro…

C#等效于Java List <?扩展类> - c#

我有泛型类的基本结构public class Parent<T> where T : Parent<T> { Action<T> Notify; } public class Child : Parent<Child> { } 我想要一个列表,以便可以将Child对象放在此处List<Parent>…

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…

将谓词<T>转换为Func <T,bool> - c#

我有一个包含成员Predicate的类,希望在Linq表达式中使用该类:using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { return m_instrumentList.All(m_filterExpression); } private IEnumerable&…

List <Dog>是List <Animal>的子类吗?为什么Java泛型不是隐式多态的? - java

我对Java泛型如何处理继承/多态感到困惑。假设以下层次结构-动物(父母)狗-猫(儿童)因此,假设我有一个方法doSomething(List<Animal> animals)。根据继承和多态性的所有规则,我假设List<Dog>是List<Animal>,而List<Cat>是List<Animal&g…