Deserialize list of dictionaries

Oct 6, 2015 at 5:30 PM
Edited Oct 6, 2015 at 5:55 PM
Hi,
I have no problem with serialize/deserialize basic class.
But I have following Json:
[
  {
    "KeyName": "keyA",
    "KeyValue": "1",
    "Category": "test"
  },
  {
    "KeyName": "keyB",
    "KeyValue": "2",
    "Category": "test"
  }
]
And converter not working
Converter.Deserialize<List<Dictionary<string, object>>>(stringResult);//or
Converter.Deserialize<Dictionary<string, object>[]>(stringResult);
Error:
System.ArgumentException: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] doesn't have a field named: KeyName
w CodeBetter.Json.Helpers.ReflectionHelper.FindField(Type type, String name)
w CodeBetter.Json.JsonDeserializer.ParseObject(Type type)
w CodeBetter.Json.JsonDeserializer.DeserializeValue(Type type)
w CodeBetter.Json.JsonDeserializer.DeserializeList(Type listType)
w CodeBetter.Json.JsonDeserializer.DeserializeValue(Type type)
w CodeBetter.Json.JsonDeserializer.Deserialize[T](JsonReader reader, String fieldPrefix)
w CodeBetter.Json.Converter.Deserialize[T](String json, String fieldPrefix)
w CodeBetter.Json.Converter.Deserialize[T](String json)
Please help.
Nov 12, 2015 at 10:47 AM
Hi, lukaszp1985.

Did you solved that trouble?
How?

Best regards.
Nov 12, 2015 at 11:33 AM
De-serializing dictionaries is not possible.
My fix is to create class with properties that I expect.
For example:
public class Result
{
    private string _KeyName;
    private string _KeyValue;
    private string _Category;
    public string KeyName
    {
        get{return _KeyName;}
        set{_KeyName = value;}
    }
    (...)
}
and then:
Converter.Deserialize<List<Result>>(stringResult,"_");
It's not very convenient, but it works.
Nov 12, 2015 at 3:59 PM
I'll check to understand if it's too hard to make.
Had you ever tried? In positive case, how was your experience?

Thank you for answer, Lukaz.


Best
Nov 12, 2015 at 9:39 PM
Yes, I'm using this method in app (Windows Mobile 5.0, Symbol 9090, .NET 2.0).
Simple method to get data after deserialization:
var data = Converter.Deserialize<List<Result>>(stringResult,"_");
MessageBox.Show(data[0].KeyName);
You can also replace List by array of object in specific class:
var data = Converter.Deserialize<Result[]>(stringResult,"_");
Nov 13, 2015 at 3:49 PM
Great idead, Lukas!

I was thinking about to change the lib, but your method is simple and rational.

Thank you for sharing.

:-)