I have this simple JSON and I was making deserializing this JSON, so I can get the elements. This is the JSON I have :
{
"QuestionIDs": [
"QID1",
"QID3"
],
"QuestionDefinitions": {
"Question1": {
"DETag": "Q1",
"Config": {
"QDescription": "UseText"
},
"COrder": [
"1",
"2",
"3"
],
"Validation": {
"Settings": {
"ForceResponse": "OFF",
"ForceResponseType": "ON",
"Type": "None"
}
}
}
},
"NextButton": null,
"PreviousButton": false
}
This is the code I've written :
[DataContract]
public class RootObject
{
[DataMember]
public Dictionary<string, Dictionary<string, string>> QuestionDefinitions { get; set; }
[DataMember]
public List<string> QuestionIDs { get; set; }
}
QuestionIDs
is working just fine. But, QuestionDefinitions
isn't working. It says that it is an empty sequence. I'm not sure what's wrong.
I want to be able to access QuestionDefinitions
.
I tried with Json.Net
. Facing the same issue. I was able to get the simple elements that are out. But, couldn't get to access the QuestionDefinitions
.
Any help would be appreciated.
EDIT :
I also tried implementing this class like this :
[DataContract]
public class QuestionDetails
{
[DataMember]
public string DETag { get; set; }
[DataMember]
[DataMember]
public List<Configuration> Config { get; set; }
[DataMember]
public List<string> ChoiceOrder { get; set; }
[DataMember]
public List<Validation> Validation { get; set; }
[DataMember]
public List<string> COrder { get; set; }
[DataMember]
public string NextButton { get; set; }
[DataMember]
public string PreviousButton { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Dictionary<string, QuestionDetails> QuestionDefinitions { get; set; }
[DataMember]
public List<string> QuestionIDs { get; set; }
}
Your class strucutre against the JSON getting returned is not correct.
When pasted your JSON in json2csharp.com the following the POCO class which generated:
public class Config
{
public string QDescription { get; set; }
}
public class Settings
{
public string ForceResponse { get; set; }
public string ForceResponseType { get; set; }
public string Type { get; set; }
}
public class Validation
{
public Settings Settings { get; set; }
}
public class Question1
{
public string DETag { get; set; }
public Config Config { get; set; }
public List<string> COrder { get; set; }
public Validation Validation { get; set; }
}
public class QuestionDefinitions
{
public Question1 Question1 { get; set; }
}
public class RootObject
{
public List<string> QuestionIDs { get; set; }
public QuestionDefinitions QuestionDefinitions { get; set; }
public object NextButton { get; set; }
public bool PreviousButton { get; set; }
}
Now using the above class as representation of your JSON structure you should be able to deserialize it in to C# object.
The following line of code would do it using NewtonSoft library:
string json = "your json result";
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
You can even do it from within Visual Studio if you have 2013 or above doing like following as described in this post :
Hope it helps.
Changing a little the code in @EhsanSajjad's answer , maybe you could use something like:
public class Config
{
public string QDescription { get; set; }
}
public class Settings
{
public string ForceResponse { get; set; }
public string ForceResponseType { get; set; }
public string Type { get; set; }
}
public class Validation
{
public Settings Settings { get; set; }
}
public class Question
{
public string DETag { get; set; }
public Config Config { get; set; }
public List<string> COrder { get; set; }
public Validation Validation { get; set; }
}
public class RootObject
{
public List<string> QuestionIDs { get; set; }
public Dictionary<string, Question> QuestionDefinitions { get; set; }
public object NextButton { get; set; }
public bool PreviousButton { get; set; }
}
Try this model
public class RootObject
{
public List<string> QuestionIDs { get; set; }
public QuestionDefinitions QuestionDefinitions { get; set; }
public object NextButton { get; set; }
public bool PreviousButton { get; set; }
}
public class QuestionDefinitions
{
public Question1 Question1 { get; set; }
public List<string> COrder { get; set; }
public Validation Validation { get; set; }
}
public class Question1 {
public string DETag { get; set; }
public Config Config { get; set; }
}
public class Config {
public string QDescription { get; set; }
}
public class Validation {
public Settings Settings { get; set; }
}
public class Settings
{
public string ForceResponse { get; set; }
public string ForceResponseType { get; set; }
public string Type { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.