简体   繁体   English

反序列化字典数组<string,object>

[英]De-serializing an array of dictionary<string,object>

I'm getting a JSON object with an string like: 我得到一个带有如下字符串的JSON对象:

"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; 我正在尝试使用DataContractSerializer并通过使DataMember合同成为public Dictionary<string,object> [] rec;类型的成员来反序列化它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]'. 无法将类型为“ System.Object”的对象转换为类型为“ System.Collections.Generic.Dictionary`2 [System.String,System.Object]”。

Can someone explain to me how I should go about deserializing this string ? 有人可以向我解释如何反序列化此字符串吗?

why not use json.net ? 为什么不使用json.net

deserialization from their docu: 从文档中反序列化:

string json = @"{""key1"":""value1"",""key2"":""value2""}";

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

Console.WriteLine(values.Count);
// 2

Console.WriteLine(values["key1"]);
// value1

which could be enhanced on classes with contain certain [Json...] -attributes ... If you are working with DataMember -attributes, you can go straight for json.net, as they support the usage of DataMember and alikes ... 可以在包含某些[Json...] -attributes的类上进行增强...如果您正在使用DataMember -attributes,则可以直接使用json.net,因为它们支持使用DataMember等。

serialization should work too - just have tried lists and alike-stuff yet which worked more than fine for me! 序列化也应该工作-只是尝试过列表和类似的东西,但对我来说还不错!

I think you should use the JavaScriptSerializer: 我认为您应该使用JavaScriptSerializer:

var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<Dictionary<string, object>>("{" + data + "}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM