2016-11-30 3 views
0

как я могу преобразовать объект JSON в коллекции, которые реализуют IEnumerable, так что я могу использовать в Еогеасппреобразования JSON объект в коллекции, которая реализует IEnumerable

ОШИБКА: «Еогеасп оператор не может работать с переменными типа„Атрибуты“ потому что «Атрибуты» не содержит публичное определение «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 является * единственным * объектом, а не массивом. Массивы заключены в квадратные скобки '[]' –

ответ

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

Я не хочу, чтобы Атрибуты были массивом, но не могу показать, как я могу заставить класс атрибутов реализовать IEnumerable интерфейс. – shomaail

+2

@Shomaali Ваш фрагмент «Атрибут» - это словарь, который обычно десериализуется как отдельный объект. Если вам нужен сам словарь вместо объекта, измените 'Атрибуты' на' Словарь <строка, строка> ' –

+0

Я согласен с PKanavos, но если вы настаиваете на том, чтобы сделать его IEnumerable, следующая ссылка поможет: http: // stackoverflow .com/вопросы/11296810/how-do-i-implement-ienumerablet – Klinger

Смежные вопросы