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

时间:2014-08-24 15:24:43

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

@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元素的值?

1 个答案:

答案 0 :(得分:0)

我真的不知道Model.Content.GetPropertyValue<ArchetypeModel>("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;
}