我试着去序列化我的字典里,看起来像一本字典:序列化和反序列化包含类关键字
private Dictionary<MetaDataKey, User> _dictionary;
其中MetaDataKey和用户类看起来像这样:
internal class User
{
public string UserName { get; set; }
public string UserPassword { get; set; }
public List<Account> Accounts { get; set; }
}
internal class Account
{
public string Subject { get; set; }
public string AccName { get; set; }
public string AccPass { get; set; }
public List<string> Notes { get; set; }
}
internal class MetaDataKey
{
public string Name { get; set; }
public string Password { get; set; }
}
我想从这样的json文件保存\加载字典到\:
private void DictionaryInit()
{
//gets the dictionary file if exists, create an empty one if not.
string path = Directory.GetCurrentDirectory() + "\\dic.json";
if (!File.Exists(path))
{
_dictionary = new Dictionary<MetaDataKey, User>();
return;
}
using (StreamReader r = new StreamReader(path))
{
string json = r.ReadToEnd();
_dictionary = JsonConvert.DeserializeObject<Dictionary<MetaDataKey, User>>(json);
}
}
public void DictionarySave()
{
//save the dictionary into dic.json file
string path = Directory.GetCurrentDirectory() + "\\dic.json";
string json = JsonConvert.SerializeObject(_dictionary);
File.WriteAllText(path, json);
}
我在装载一个新的记录到字典,并试图挽救它,我得到:
{"WpfApplication2.MetaDataKey":{"UserName":"Enter Name","UserPassword":"Enter Password","Accounts":null}}
代替:
{"WpfApplication2.MetaDataKey":{"Name":"Enter Name","Password":"Enter Password"},"WpfApplication2.User":{"UserName":"Enter Name","UserPassword":"Enter Password","Accounts":null}}
,你可以告诉我得到在MetaDataKey类用户的领域。 即使我修复它manualy我仍然得到异常:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
当我试图加载非空文件。 总之,2个问题: 1.糟糕的json节约。 2.坏JSON装载
我们可以看到一个json文件包含的例子吗? – maccettura
第一次是空的,我添加一个记录后,我得到这个文件:{“WpfApplication2.MetaDataKey”:{“UserName”:“Enter Name”,“UserPassword”:“Enter Password”,“Accounts”: null}} (与iive发布相同) –
它看起来像Newtonsoft JSON序列化程序使用键对象的ToString表示形式作为键。默认的ToString表示是完整的类名(= namespace.classname)。 – ckuri