locked
how to deserialize json to a dictionary or keyvaluepair? RRS feed

  • Question

  • I can't seem to find a good tutorial or example on deserializing a json file to a dictionary that works for me. I get a "arguementExeption was unhandled" error when reading the stream. I've read that it might be easier to deserialize to a keyvaluepair. dictionary or keyvaluepair would work fine for me but when I change "conversionRates" to a KeyValuePair, the key is read as null and value read as 0. unitTypeName gets read fine. Can anyone point me in the right direction?

    Here is are the properties of my class:

    [DataMember]
    public string unitTypeName { get; set; }

    [DataMember]
    public KeyValuePair conversionRates;

    Here is the json file I'm using:

    {"units":[{"unitTypeName":"Newton","conversionRates":{"Newton":"1","Pound":"0.224808943"}},{"unitTypeName":"Pound","conversionRates":{"Newton":"4.44822162","Pound":"1"}]}

    Here is the deserialization:

    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(UnitResults));
    UnitResults results = (UnitResults)dataContractJsonSerializer.ReadObject(stream);
    Saturday, October 30, 2010 6:06 PM