在MVC和Razor中访问嵌套的JSON元素
我对MVC、JSON、Razor甚至.Net都是新手,所以请对我放松点 我一直在工作,没有任何问题 然后,我开始使用代码作为基础,将嵌套的JSON元素检索到HTML表中。我已经成功地为第一层嵌套元素做到了这一点,但在第二层嵌套元素中遇到了困难 这是一个示例JSON文档在MVC和Razor中访问嵌套的JSON元素,json,asp.net-mvc,azure,razor,Json,Asp.net Mvc,Azure,Razor,我对MVC、JSON、Razor甚至.Net都是新手,所以请对我放松点 我一直在工作,没有任何问题 然后,我开始使用代码作为基础,将嵌套的JSON元素检索到HTML表中。我已经成功地为第一层嵌套元素做到了这一点,但在第二层嵌套元素中遇到了困难 这是一个示例JSON文档 { "title": "War Diary for 2nd Battalion Scots Guards September 1943", "date": "September 1943", "unit": "2nd Battal
{
"title": "War Diary for 2nd Battalion Scots Guards September 1943",
"date": "September 1943",
"unit": "2nd Battalion Scots Guards",
"brigade": "201 Guards Brigade",
"division": "56 Infantry Division",
"entries": [
{
"day": "1st",
"location": "Tripoli",
"time": "",
"text": [
{
"p": "The L.S.T. party march to the Race Course prior to embarkation...."
}
]
},
{
"day": "2nd",
"location": "",
"time": "",
"text": [
{
"p": "A two hours march brings the L.S.T. party to the quay..."}
]
},
{
"day": "3rd",
"location": "",
"time": "",
"text": [
{
"p": "The \"fighting four hundred\"; or the two L.C.I's parties march..." },
"p": "The L.C.I. parties embark. Last minute-farewells. Convoy sails after dark..."}
]
}
}
]
}
这就是模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
namespace todo.Models
{
public class WarDiary
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "date")]
public string Date { get; set; }
[JsonProperty(PropertyName = "unit")]
public string Unit { get; set; }
[JsonProperty(PropertyName = "brigade")]
public string Brigade { get; set; }
[JsonProperty(PropertyName = "division")]
public string Division { get; set; }
[JsonProperty(PropertyName = "entries")]
public Entry [] Entries { get; set; }
}
public class Entry
{
[JsonProperty(PropertyName = "text")]
public Details[] Text { get; set; }
[JsonProperty(PropertyName = "day")]
public string Day { get; set; }
[JsonProperty(PropertyName = "time")]
public string Time { get; set; }
[JsonProperty(PropertyName = "location")]
public string Location { get; set; }
}
public class Details
{
[JsonProperty(PropertyName = "p")]
public string P { get; set; }
}
}
意见是:
@model todo.Models.WarDiary
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Details</h2>
<div>
<h4>WarDiary</h4>
<hr />
<table class="table">
<tr>
<th>
Location
</th>
<th>
Day
</th>
<th>
Time
</th>
<th>
Summary
</th>
</tr>
@foreach (var entry in Model.Entries)
{
<tr>
<td>
@entry.Location
</td>
<td>
@entry.Day
</td>
<td>
@entry.Time
</td>
<td>
@foreach (var p in Model.Entries.Text)
{
<p>
@p.P
</p>
}
</td>
</tr>
}
</table>
我得到了错误信息
“Entry[]”不包含“Text”的定义,并且找不到接受“Entry[]”类型的第一个参数的扩展方法“Text”(是否缺少using指令或程序集引用?)
我曾尝试将类细节嵌套在条目中,但似乎也有同样的问题。我假设它与模型的定义有关
感谢您的建议
型号。条目
的类型为Entry[]
。每个条目
都有一个文本
属性。但是,您正试图从数组本身访问属性Text
。您的代码应该是:
@foreach (var entry in Model.Entries)
{
<tr>
<td>
@entry.Location
</td>
<td>
@entry.Day
</td>
<td>
@entry.Time
</td>
<td>
@foreach (var p in entry.Text)
{
<p>
@p.P
</p>
}
</td>
</tr>
}
@foreach(Model.Entries中的var条目)
{
@入口,位置
@入境日期
@进场时间
@foreach(entry.Text中的var p)
{
@p、 p
}
}
完美。谢谢
@foreach (var entry in Model.Entries)
{
<tr>
<td>
@entry.Location
</td>
<td>
@entry.Day
</td>
<td>
@entry.Time
</td>
<td>
@foreach (var p in entry.Text)
{
<p>
@p.P
</p>
}
</td>
</tr>
}