将JSON对象转换为实现IEnumerable的集合

时间:2016-11-30 08:25:24

标签: c# json ienumerable

如何将JSON对象转换为实现IEnumerable的集合,以便我可以在 foreach中使用

错误:“foreach语句无法对”属性“类型的变量进行操作,因为”属性“不包含”GetEnumerator“的公共定义” 遍历属性的.net代码:

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
            {}

JSON文件的一部分:

  {  "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"
}

} }

生成的类

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; }}

1 个答案:

答案 0 :(得分:1)

在您的json示例中,“Attributes”不是数组。 如果要枚举属性,则需要将其定义为数组:

 "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"
 } ],

或者,您需要创建属性类implement IEnumerable interface

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

相关问题