【问题标题】:Get element from JSON string in foreach从 foreach 中的 JSON 字符串中获取元素
【发布时间】:2014-08-24 15:24:43
【问题描述】:
@foreach (var fieldset in Model.Content.GetPropertyValue<ArchetypeModel>("MainMenuDT"))
{
    <li>@fieldset.GetValue("linkObj")</li> 
}

这个输出:

[  
   {  
      "caption":"gjhg",
      "link":"http://localhost:2081/",
      "newWindow":false,
      "edit":false,
      "isInternal":false,
      "type":"external",
      "title":"gjhg"
   }
]

如何从 JSON 字符串中获取 linkObj.link 元素的值?

【问题讨论】:

    标签: c# asp.net json json.net


    【解决方案1】:

    我真的不知道Model.Content.GetPropertyValue&lt;ArchetypeModel&gt;("MainMenuDT") 返回什么,但是使用JSON.net 解析json 真的很容易:

    public class MyObject
    {
      public string caption {get;set;}
      public string link {get;set;}
      public bool newWindow {get;set;}
      public bool edit {get;set;}
      public bool isInternal {get;set;}
      public string type{get;set;}
      public string title{get;set;}
    }
    
    List<MyObject> obj = JsonConvert.DeserializeObject<List<MyObject>>(json); // json is a string
    if(obj.Count > 0)
    {
       string link = obj[0].link;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2021-11-26
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多