shomaail shomaail - 2 days ago 5
JSON Question

convert JSON object to collections which implements IEnumerable

how can I convert JSON object to Collections that implement IEnumerable so that I can use in foreach

ERROR: "foreach statement cannot operate on variables of type 'Attributes' because 'Attributes' does not contain a public definition for 'GetEnumerator' "
the .net code to traverse Attributes:

var jsonData = JsonConvert.DeserializeObject<Rootobject>(json);
//RootObject is the class generated from Json using Paste JSON as Classes
var att = jsonData.AnswerTA.Attributes;
foreach (var item in att)<-- This is giving error
{}


Part of JSON file:

{ "FormTitle": "This is Form Title from JSON",
"TitleQuestion1": "This s the Title of Question 1",
"TextQuestion1": "1- This is the text of Quextion umber 1", "AnswerRadioButton": { "visibleRB": "true", "titleRB": "Radio Button Title",
"FieldsetRB": "yes",
"optionRB": [
{
"text": "text1",
"value": "v1",
"checked": "false"
},
{
"text": "text2",
"value": "v2",
"checked": "false"
},
{
"text": "text3",
"value": "v3",
"checked": "false"
},
{
"text": "text4",
"value": "v4",
"checked": "true"
},
{
"text": "text5",
"value": "v4",
"checked": "false"
}
]
}, "AnswerCheckBox": { "visibleCB": "true", "titleCB": "Check box Answer Title", "FieldsetCB": "yes", "optionCB": [
{ "text": "ch text1", "value": "v1", "checked": "false" }, { "text": "tzxcsdcext2", "value": "v2",
"checked": "false"
},
{ "text": "text3", "value": "v3", "checked": "false"
},
{ "text": "text4", "value": "v4", "checked": "true"
}
]}, "AnswerDropDownList": { "visibleDDl": "true", "required": "no", "titleDDL": "Title of Drop Down List ", "FieldsetDDL": "yes", "optionDDL": [ { "text": "Select", "value": "" },
{ "text": "IE", "value": "IE" }, {
"text": "Safari", "value": "Safari" },
{ "text": "Chrome", "value": "Chrome"
} ] }, "AnswerTB": { "visibleTB": "true", "required": "no",
"titleTB": "Title of TB ", "FieldsetTB": "yes" },
"AnswerTA": {
"visibleTA": "true",
"required": "no",
"titleTA": "Title of TA ",
"FieldsetTA": "yes",
"Attributes": {
"placeholder": "this is the watermark",
"title": "this is tooltip",
"maxlength": "10",
"minlength": "5",
"required": "yes"
},
"Style": {
"height": "50px",
"width" : "5px"
}


}
}

Classes generated

public class Rootobject{
public string FormTitle { get; set; }
public string TitleQuestion1 { get; set; }
public string TextQuestion1 { get; set; }
public Answerradiobutton AnswerRadioButton { get; set; }
public Answercheckbox AnswerCheckBox { get; set; }
public Answerdropdownlist AnswerDropDownList { get; set; }
public Answertb AnswerTB { get; set; }
public Answerta AnswerTA { get; set; }
}

public class Answerta{
public string visibleTA { get; set; }
public string required { get; set; }
public string titleTA { get; set; }
public string FieldsetTA { get; set; }
public Attributes Attributes { get; set; }
public Style Style { get; set; }
}
public class Attributes{
public string placeholder { get; set; }
public string title { get; set; }
public string maxlength { get; set; }
public string minlength { get; set; }
public string required { get; set; }}

Answer

In your json sample "Attributes" is not an array. If you want to enumerate Attributes it needs to be defined as an array:

 "Attributes":[ {
   "placeholder": "this is the watermark",
   "title": "this is tooltip",
   "maxlength": "10",
   "minlength": "5",
   "required": "yes"
 },
 { 
   "placeholder": "this is the watermark",
   "title": "this is tooltip",
   "maxlength": "10",
   "minlength": "5",
   "required": "yes"
 } ],

Or, you need to make Attributes class implement IEnumerable interface.

Also, you can enumerate the properties of Attributes by using reflection

Comments