I am trying to get the type of serialized C# object in JSON Text ( $type in JSON text) without deserialising the JSON text to object again . can you please suggest what are all the option do i have?
我试图在JSON文本中获取序列化C#对象的类型(JSON文本中的$ type),而不再将JSON文本反序列化为对象。你能建议我有什么选择吗?
I am using Newtonsoft library for serialization and serialization.
我正在使用Newtonsoft库进行序列化和序列化。
Thanks in advance
提前致谢
2 个解决方案
#1
1
I don't use Newtonsoft library. However, assuming that $type is either at the start of the file or at the end I'd probably use string functions (psudocode below) which'd be quite fast.
我不使用Newtonsoft库。但是,假设$ type位于文件的开头或结尾处,我可能会使用字符串函数(下面的psudocode),这将非常快。
find $type
-
i=find next colon
我找到下一个冒号
-
j=find next comma
j =找到下一个逗号
-
grab token between i and j
在i和j之间抓取令牌
-
trim that token
修剪那个令牌
-
do something useful with the token. Make a type out of it with reflection?
用令牌做一些有用的事情。用反射做出一个类型吗?
How does that sound? While you are at it you could write an extension method. Hit +1 several times and I'll think about writing the code ;-)
听上去怎么样?当你在它时你可以写一个扩展方法。点击+1几次,我会考虑编写代码;-)
#2
0
Its fairly simple using the Newtonsoft libarary.
使用Newtonsoft图书馆相当简单。
JObject json = JObject.Parse(JasonText);
var type= json["type"];
#1
1
I don't use Newtonsoft library. However, assuming that $type is either at the start of the file or at the end I'd probably use string functions (psudocode below) which'd be quite fast.
我不使用Newtonsoft库。但是,假设$ type位于文件的开头或结尾处,我可能会使用字符串函数(下面的psudocode),这将非常快。
find $type
-
i=find next colon
我找到下一个冒号
-
j=find next comma
j =找到下一个逗号
-
grab token between i and j
在i和j之间抓取令牌
-
trim that token
修剪那个令牌
-
do something useful with the token. Make a type out of it with reflection?
用令牌做一些有用的事情。用反射做出一个类型吗?
How does that sound? While you are at it you could write an extension method. Hit +1 several times and I'll think about writing the code ;-)
听上去怎么样?当你在它时你可以写一个扩展方法。点击+1几次,我会考虑编写代码;-)
#2
0
Its fairly simple using the Newtonsoft libarary.
使用Newtonsoft图书馆相当简单。
JObject json = JObject.Parse(JasonText);
var type= json["type"];