反序列化字典数组
我得到一个带有字符串的 JSON 对象,如下所示:
"rec":[
{"f":["T","R"],"u":316,"fName":"test","lName":"test2"},
{"f":["C","R"],"u":990,"fName":"beth","lName":"tin"}
],
我正在尝试使用 DataContractSerializer 并将 DataMember 合约转换为 public Dictionary
但我收到如下错误:
“System.Object”类型的对象不能 被转换为类型 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'。
有人可以向我解释一下我应该如何反序列化这个字符串吗?
I'm getting a JSON object with an string like:
"rec":[
{"f":["T","R"],"u":316,"fName":"test","lName":"test2"},
{"f":["C","R"],"u":990,"fName":"beth","lName":"tin"}
],
I'm trying to de-serialize it using the DataContractSerializer and by having a DataMember contract into a member of type public Dictionary<string,object> [] rec;
But i get an error like:
Object of type 'System.Object' cannot
be converted to type
'System.Collections.Generic.Dictionary`2[System.String,System.Object]'.
Can someone explain to me how I should go about deserializing this string ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该使用 JavaScriptSerializer:
I think you should use the JavaScriptSerializer:
为什么不使用 json.net ?
从他们的文档中反序列化:
可以在包含某些
[Json...]
-attributes 的类上得到增强...如果您正在使用DataMember
-attributes,您可以直接访问 json.net,因为它们支持使用DataMember
等...序列化也应该有效 - 只是尝试过列表和类似的东西,但对我来说效果非常好!
why not use json.net?
deserialization from their docu:
which could be enhanced on classes with contain certain
[Json...]
-attributes ... If you are working withDataMember
-attributes, you can go straight for json.net, as they support the usage ofDataMember
and alikes ...serialization should work too - just have tried lists and alike-stuff yet which worked more than fine for me!