2016-11-30 58 views
0

我怎麼能轉換成JSON對象來實現IEnumerable的集合,這樣我可以在的foreach使用轉換JSON對象的集合,它實現了IEnumerable

ERROR:「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; }} 
+2

發佈您的課程和完整的json。 –

+0

Json字符串中的「Attributes」文本是* single *對象,而不是數組。數組被括在方括號中[]' –

回答

1

在你的JSON樣品 「屬性」 不是數組。 如果你想枚舉屬性需要將其定義爲一個數組:

"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

+0

我不想讓Attributes成爲一個數組,但是你能告訴我如何讓Attributes類實現IEnumerable接口。 – shomaail

+2

@Shomaali您的'屬性'片段是典型的反序列化爲單個對象的字典。如果你想要字典本身而不是對象,請將'Attributes'更改爲'Dictionary ' –

+0

我同意PKanavos,但是如果你堅持讓它IEnumerable,以下鏈接將有所幫助:http:// stackoverflow .com/questions/11296810/how-do-i-implement -ienumerablet – Klinger

相關問題