C#Newtonsoft JSON序列化 [英] C# Newtonsoft json serialisation
问题描述
大家好,
我正在尝试反序列化我之前用
序列化的json字符串
Newtonsoft Json库.
Hi everybody,
I am trying to deserialize a json string which I have serialized before with the
Newtonsoft Json library.
using (StreamReader sr = new StreamReader(s))
{
json = sr.ReadToEnd();
}
object o = JsonConvert.DeserializeObject(json);
return o;
反序列化后,var o包含一个JObject,但是我想要源文件
我已序列化的对象.我该如何找回真实物体.
我知道有一个JsonConvert.DeserializeObject< t>()函数,但是我不知道它是什么类型的对象,因为我正在序列化许多不同的对象.
当我序列化对象时,我将对象的类型存储在json字符串中,
所以如何告诉序列化程序使用存储的类型进行反序列化.
在此先感谢
Manu
After the deserialisation the var o contains a JObject, but I want the source
object which I have serialized. How can I get the real object back.
I know there is a JsonConvert.DeserializeObject<t>() function, but I do not know what type of object it is, because I am serializing a lot of different objects.
When I serialize my objects I store the type of the object in the json string,
so how can I tell the serializer to use the stored type for deserializing.
Thanks in advance
Manu
推荐答案
答案如何: ^ ]
JsonSerializerSettings set = new JsonSerializerSettings();
set.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
set.TypeNameHandling = TypeNameHandling.All;
set.Formatting = Newtonsoft.Json.Formatting.None;
使用此设置进行序列化和反序列化时
它可以工作,现在我得到的是真正的对象,而不是JObject.
When using this settings for serializing and deserializing
it works, now i get the real object and not a JObject.
这篇关于C#Newtonsoft JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!