如何使用Json.Net在C#中序列化和反序列化此类?

时间:2016-12-21 10:33:46

标签: c# serialization json.net deserialization

我想序列化和反序列化以下类:

public class SettingsToSyncModel
{
    public SettingsToSyncModel()
    {
        this.DatabasesToSync = new List<DatabasesToSyncModel>();
        this.TablesToSync = new List<TablesToSyncModel>();
    }

    public SettingsToSyncModel(IList<DatabasesToSyncModel> databases, IList<TablesToSyncModel> tables)
    {
        this.DatabasesToSync = new List<DatabasesToSyncModel>();
        this.TablesToSync = new List<TablesToSyncModel>();
        if (databases.Any()) DatabasesToSync = databases;
        if (tables.Any()) TablesToSync = tables;
    }
    [JsonProperty("databasesToSync")]
    public IList<DatabasesToSyncModel> DatabasesToSync { get; set; }

    [JsonProperty("tablesToSync")]
    public IList<TablesToSyncModel> TablesToSync { get; set; }
}

public class DatabasesToSyncModel
{
    public DatabasesToSyncModel() { }

    public DatabasesToSyncModel(int databaseId, string databaseName, string databaseConnection)
    {
        this.DatabaseId = databaseId;
        this.DatabaseName = databaseName;
        this.DatabaseConnection = databaseConnection;
    }
    [JsonProperty("database_id")]
    public int DatabaseId { get; set; }

    [JsonProperty("database_name")]
    public string DatabaseName { get; set; }

    [JsonProperty("database_connection")]
    public string DatabaseConnection { get; set; }
}

public class TablesToSyncModel
{
    public TablesToSyncModel() { }


    public TablesToSyncModel(int tableId, string tableName, int databaseId)
    {
        this.TableId = tableId;
        this.TableName = tableName;
        this.DatabaseId = databaseId;
    }

    [JsonProperty("table_id")]
    public int TableId { get; set; }

    [JsonProperty("table_name")]
    public string TableName { get; set; }
    [JsonProperty("database_id")]
    public int DatabaseId { get; set; }
}

我像这样序列化:

var jsonSettings = JsonConvert.SerializeObject(settingsToSync);

我得到一个这样的字符串:

"{\"databasesToSync\":[{\"database_id\":1,\"database_name\":\"prova\",\"database_connection\":\"test_connection_prova\"},{\"database_id\":3,\"database_name\":\"prova2\",\"database_connection\":\"test_connection_prova2\"}],\"tablesToSync\":[{\"table_id\":1,\"table_name\":\"contatti\",\"database_id\":1},{\"table_id\":2,\"table_name\":\"produzione\",\"database_id\":1},{\"table_id\":3,\"table_name\":\"lavorazioni\",\"database_id\":3},{\"table_id\":4,\"table_name\":\"ordini\",\"database_id\":3}]}"

我无法使用相同的类

反序列化它
var deserialized = JsonConvert.DeserializeObject<SettingsToSyncModel>(jsonResponse);

堆栈追踪:

in Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   in Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   in Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)

异常错误:

Error converting value to type 'SettingsToSyncModel'

它有什么问题?

0 个答案:

没有答案