Reputation: 5737
I have problems deserializing this JSON data structure comming from an API. We have no possibilities to change the JSON data, so I need to serialize it as it is.
{"success":"1","return":{"balances_available":{"ALF":"0.00000000","AMC":"0.00000000","ADT":"0.00000000","ANC":"0.00000000"}}}
Currently I try this
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Response));
balances = serializer.ReadObject(ms) as Response;
[DataContract]
public class Response
{
[DataMember(Name = "success")]
public int Status { get; set; }
[DataMember(Name = "return")]
public Balance Return { get; set; }
}
[DataContract]
public class Balance
{
[DataMember(Name = "balances_available")]
public Dictionary<string, string> BalancesAvailable { get; set; }
}
But my dictionary get 0 items, so it does not look like it is beeing serialized. Are there no other way of fixing this than manually creating a class where I define all balance items (ALF, AMC, etc.)?
Upvotes: 1
Views: 1028
Reputation: 3279
You must add DataContractJsonSerializerSettings
like this:
var settings = new DataContractJsonSerializerSettings
{
UseSimpleDictionaryFormat = true
};
var serializer = new DataContractJsonSerializer(typeof(Response), settings);
And this will be work. Good luck!
Upvotes: 2