【问题标题】:convert JSON object to collections which implements IEnumerable将 JSON 对象转换为实现 IEnumerable 的集合
【发布时间】:2017-04-14 11:41:40
【问题描述】:

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

错误:“foreach 语句无法对 'Attributes' 类型的变量进行操作,因为 'Attributes' 不包含 '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; }}

【问题讨论】:

  • 发布您的课程和完整的 json。
  • Json 字符串中的 Attributes 文本是 single 对象,而不是数组。数组用方括号括起来[]

标签: c# json ienumerable


【解决方案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

【讨论】:

  • 我不想让 Attributes 成为一个数组,但是你能告诉我如何让 Attributes 类实现 IEnumerable 接口。
  • @Shomaali 您的Attribute sn-p 是一个字典,通常反序列化为单个对象。如果您想要字典本身而不是对象,请将 Attributes 更改为 Dictionary&lt;string,string&gt;
  • 我同意 PKanavos,但如果您坚持将其设为 IEnumerable,以下链接将有所帮助:stackoverflow.com/questions/11296810/…
  • @Panagiotis Kanavos 我也同意你的看法。你能告诉我如何将属性更改为字典
猜你喜欢
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多