De-serializing an array of dictionary<string,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]'.
C开发者_如何学JAVAan someone explain to me how I should go about deserializing this string ?
why not use 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 ...
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:
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<Dictionary<string, object>>("{" + data + "}")
精彩评论