关于c#:将JSON对象转换为实现IEnumerable的集合

convert JSON object to collections which implements IEnumerable

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

错误:"foreach语句无法对"attributes"类型的变量进行操作,因为"attributes"不包含"getEnumerator"的公共定义。"要遍历的.NET代码属性:

1
2
3
4
5
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文件的一部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  { "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"
}

}}

生成的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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; }}


在JSON示例中,"attributes"不是数组。如果要枚举属性,需要将其定义为数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"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"
 } ],

或者,您需要使属性类实现IEnumerable接口。

此外,还可以使用反射枚举属性的属性