简体   繁体   中英

Accessing second level JSON array in ASP.NET MVC 5

Consider the following array of JSON objects:

JSON:

{
   status: "ok",
   count: 10,
   count_total: 642,
   pages: 65,
 - posts: [
    - {
         id: 681,
         type: "post",
         slug: "lorem_ipsum_dolor_sit_amet",
         url: "/2015/09/lorem_ipsum_dolor_sit_amet/",
         status: "publish",
         title: "Lorem ipsum dolor sit amet",
         title_plain: "Lorem ipsum dolor sit amet",
         content: "Lorem ipsum dolor sit amet",
         excerpt: "Lorem ipsum dolor sit amet",
         date: "2015-09-14 10:06:06",
         modified: "2015-09-22 16:13:55",
       - categories: [
          - {
               id: 23,
               slug: "my_category",
               title: "myCategory",
               description: "",
               parent: 0,
               post_count: 138
            }
         ],
       - attachments: [
          - {
               id: 689,
               url: "http://example.com/wp-content/uploads/example_01.jpg",
               slug: "example_01",
               title: "example_01",
               description: "",
               caption: "",
               parent: 681,
               mime_type: "image/jpeg",
             - images: {
                - full: {
                           url: "/wp-content/uploads/sites/14/2015/09/example_full_01.jpg",
                           width: 952,
                           height: 693
                        }
                       }
           }
         ]
      }
   ]
}

Here's my controller:

...

public ActionResult News()
{
    ViewBag.News = "";

    try
    {
        dynamic News = new Json("http://www.example.com/api/get_recent_posts/").getJson();
        ViewBag.News = News.@posts;
        ViewBag.total = News.@posts.Count > 3 ? count : News.@posts.Count;
    }
    catch
    {
        Response.Cache.SetExpires(DateTime.Now.AddHours(1));
    }

    return View();
}

...

Here's my view:

...

@if (ViewBag.News != null)
{
    for (int i = 0; i < ViewBag.total; i++)
    {
        <div class="news">
            <a href="@Html.Raw(ViewBag.News[i].url)"><img src="@Html.Raw(ViewBag.News[i].attachments.images.full.url)" /></a>
            <div class="description">
                <h3><a href="@Html.Raw(ViewBag.News[i].url)">@Html.Raw(ViewBag.News[i].title)</a></h3>
                @Html.Raw(ViewBag.News[i].excerpt)
            </div>
        </div>
    }
}

...

The three lines below work perfectly:

@Html.Raw(ViewBag.News[i].url)
@Html.Raw(ViewBag.News[i].title)
@Html.Raw(ViewBag.News[i].excerpt)

I would like to get the URL of an image in the "attachments" section (posts > attachments > images > full > url), but the following line doesn't work:

@Html.Raw(ViewBag.News[i].attachments.images.full.url)

Thank you.

In your JSON, attachments is an array. You need to access it with a bracket and an index. If there's only 1 item in the array, try:

@Html.Raw(ViewBag.News[i].attachments[0].images.full.url)

Also, if you control the JSON, and that really is the case, make it an object instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM