从foreach中的JSON字符串中获取元素

本文关键字:获取 元素 字符串 JSON foreach 中的 | 更新日期: 2023-09-27 18:19:13

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

如何获取linkObj的值?链接元素从JSON字符串?

从foreach中的JSON字符串中获取元素

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

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