2016-11-30 1 views
0

comment puis-je convertir objet JSON aux collections qui mettent en œuvre IEnumerable afin que je puisse utiliser dans foreachconvertir objet JSON aux collections qui implémente IEnumerable

ERREUR: « foreach ne peut pas fonctionner sur des variables de type « Attributs » parce que 'attributs' ne contient pas de définition publique pour «GetEnumerator» le code .net pour traverser Attributs:

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

partie du fichier 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" 
} 

}}

classes générées

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

Publiez vos cours et json complet. –

+0

Le texte 'Attributes' de votre chaîne JSON est un objet * single *, pas un tableau. Les tableaux sont entourés de crochets '[]' –

Répondre

1

Dans votre exemple JSON "Attributs" est pas un tableau. Si vous voulez énumérer les attributs, il doit être défini comme un tableau:

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

Ou, vous devez faire des attributs de classe implement IEnumerable interface.

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

+0

Je ne veux pas faire d'Attributs un tableau, Cependant, pouvez-vous me montrer comment faire pour que la classe Attributs implémente l'interface IEnumerable. – shomaail

+2

@Shomaali Votre extrait 'Attribute' est un dictionnaire généralement désérialisé en un seul objet. Si vous voulez le dictionnaire lui-même à la place de l'objet, changez 'Attributes' en' Dictionary ' –

+0

Je suis d'accord avec PKanavos, mais si vous insistez pour le rendre IEnumerable, le lien suivant vous aidera: http: // stackoverflow .com/questions/11296810/how-do-i-implement-ienumerablet – Klinger