' Newtonsoft.Json.Linq.JArray' to' System.Web.UI.WebControls.ListItemCollection'

时间:2016-11-29 08:02:58

标签: c# asp.net json

我有一个Json文件,如下所示。我想只将JSON文件中的选项节点转换为C# ListItemCollection 对象

ASP.net文件:

   var path = Server.MapPath(@"~/json.json");
    using (StreamReader r = new StreamReader(path,false))
    {                      
        string json = r.ReadToEnd();
        dynamic arr = JsonConvert.DeserializeObject(json);
        ListItemCollection licRB = new ListItemCollection();  
        licRB = arr.AnswerRadioButton.option;<<-- run time error is produced here 
    }

JSON文件:

{   "FormTitle": "This is Form Title from JSON",
  "TitleQuestion1": "This is the Title of Question 1",
  "TextQuestion1": "1- This is the text of Quextion Number 1",
  "AnswerRadioButton": {
"visible": "true",
"title": "Radio Button Title",
"FieldsetRBStyle": { "border": "1px" },
"option" : [
  {
    "text": "text1",
    "value": "v1",
    "checked": "false"
  },
  {
    "text": "text2",
    "value": "v2",
    "checked": "true"
  },
  {
    "text": "text3",
    "value": "v3",
    "checked": "false"
  },
  {
    "text": "text4",
    "value": "v4",
    "checked": "true"
  }
]  }}

1 个答案:

答案 0 :(得分:1)

假设您拥有所需的类(例如使用this工具),您可以访问以下选项:

var test = JsonConvert.DeserializeObject<RootObject>(json);
var options = test.AnswerRadioButton.option;