Reputation: 2265
I have following JSON data, class and code but it doesn't working. I am getting List of 3 object but all are NULL. Can anybody please suggest me what I am missing here?
JSON data:
[
{
"donotpostalmail": {
"Do Not Allow": {
"Do Not Allow": 1
},
"Allow": {
"Allow": 0
}
}
},
{
"familystatuscode": {
"Single": {
"Single": 1
},
"Married": {
"Married": 2
},
"Divorced": {
"Divorced": 3
},
"Widowed": {
"Widowed": 4
}
}
},
{
"preferredcontactmethodcode": {
"Any": {
"Any": 1
},
"Email": {
"Email": 2
},
"Phone": {
"Phone": 3
},
"Fax": {
"Fax": 4
},
"Mail": {
"Mail": 5
}
}
}
]
Class:
public class ResponseDataOfOptions
{
public OptionsList mainList { get; set; }
}
public class OptionsList
{
public Dictionary<string, Options> optionList { get; set; }
}
public class Options
{
public Dictionary<string, int> options { get; set; }
}
.cs file Code:
List<ResponseDataOfOptions> optionList = JsonConvert.DeserializeObject<List<ResponseDataOfOptions>>(objResponse.ResponseDataOfOptions);
Upvotes: 0
Views: 87
Reputation: 1719
You cannot use objects in this case because every object has different property names. The only way to deserialize this JSON is as follows.
JsonConvert.DeserializeObject<List<
Dictionary<string,
Dictionary<string,
Dictionary<string, int>
>
>
>>(objResponse.ResponseDataOfOptions);
It is a list of dictionaries, in fact there are three levels of dictionaries. It's not a very friendly structure to work with data but that would do the deserialize.
You might also be interested in using some property detection using JObject
and JToken
and then inflate appropriate data transfer objects.
JObject list = JObject.Parse(objResponse.ResponseDataOfOptions);
foreach (var item in list) {
JToken token = jobj["donotpostalmail"];
if (token != null) {
// inflate the corresponding data type.
}
}
Upvotes: 2
Reputation: 355
You cannot use an array that contains 3 different objects types. If you want to do this, I recommend to use a container object instead of an array:
{
"responseDataOfOptions": {
"donotpostalmail": {
"Do Not Allow": {
"Do Not Allow": 1
},
"Allow": {
"Allow": 0
}
}
},
"optionsList": {
"familystatuscode": {
"Single": {
"Single": 1
},
"Married": {
"Married": 2
},
"Divorced": {
"Divorced": 3
},
"Widowed": {
"Widowed": 4
}
}
},
"options": {
"preferredcontactmethodcode": {
"Any": {
"Any": 1
},
"Email": {
"Email": 2
},
"Phone": {
"Phone": 3
},
"Fax": {
"Fax": 4
},
"Mail": {
"Mail": 5
}
}
}
}
The corresponding C# class must look like this:
public class JsonResponse {
public ResponseDataOfOptions responseDataOfOptions{ get; set; }
public OptionsList optionsList{ get; set; }
public Options options { get; set; }
}
Upvotes: 1
Reputation: 787
please check your json string objResponse.ResponseDataOfOptions
format whether is correct, maybe there are some "
in your string, if yes, you need to replace these "
as below:
List<ResponseDataOfOptions> optionList = JsonConvert.DeserializeObject<List<ResponseDataOfOptions>>(objResponse.ResponseDataOfOptions.Replace(""", "\""));
Upvotes: 0