在MVC和Razor中访问嵌套的JSON元素 [英] Accessing nested JSON elements in MVC and Razor

查看:42
本文介绍了在MVC和Razor中访问嵌套的JSON元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC,JSON,Razor和.NET还是很陌生-所以对我轻松一点.

I'm very new to MVC, JSON, Razor and indeed .Net - so go easy on me.

我一直在通过 CosmosDB演示进行工作微软,没有问题.

I have been working through the CosmosDB demo from Microsoft with no problems.

然后,我开始使用代码作为基础,以将嵌套的JSON元素检索到HTML表中.我已经成功地为嵌套元素的第一层做到了这一点,但是在第二层上却苦苦挣扎.

I then started using the code as a base to retrieve nested JSON elements into a HTML table. I have managed to do this successfully for the first level of nested elements but have struggled with the second level.

这是一个示例JSON文档.

This a sample JSON document.

{
"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中的元素(日期,时间,位置),但是一旦尝试访问Details中的元素(嵌套在Text之外),我似乎就无法引用它.

I can successfully retrieve the elements in Entry (day, time, location) but once I try to access those in Details (nested off Text) I can't seem to reference it.

问题是此行:

@foreach (var p in Model.Entries.Text)

我收到错误

'Entry []'不包含'Text'的定义,并且找不到扩展方法'Text'接受类型为'Entry []'的第一个参数(您是否缺少using指令或程序集引用?).

'Entry[]' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'Entry[]' could be found (are you missing a using directive or an assembly reference?).

我尝试在Entry中嵌套类Details,但似乎有相同的问题.我假设它与模型的定义有关.

I have tried nesting the class Details within Entry but seem to have the same issue. I'm assuming it is related to the definition of the model.

感谢您收到任何建议!

推荐答案

Model.Entries 的类型为 Entry [] .每个 Entry 都有一个 Text 属性.但是,您试图从数组本身访问属性 Text .您的代码应为:

Model.Entries is of type Entry[]. Each Entry has a Text property. But, you are trying to access the property Text from the array itself. Your code should be:

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

这篇关于在MVC和Razor中访问嵌套的JSON元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆