如何从 ASP.NET MVC 中的嵌套 json 结果读取值

我成功地从这个简单的 json 中得到了结果,并使用以下代码显示在视图中。


物种控制器.cs


using diversity.Models;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using LinqToWiki.Download;

using LinqToWiki.Generated;

using LinqToWiki;

using System.Text;

using RestSharp;

using Newtonsoft.Json.Linq;

using System.Net.Http;

using Newtonsoft.Json;


namespace diversity.Controllers

{

 public class SpeciesController : Controller

 {

  public async System.Threading.Tasks.Task<ActionResult> SpeciesDetails(){


    HttpClient client = new HttpClient();

    List<Species> datalist = new List<Species>();


    try

    {

        HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts/1");

        response.EnsureSuccessStatusCode();

        string responseBody = await response.Content.ReadAsStringAsync();


        datalist = JsonConvert.DeserializeObject<List<Species>>(responseBody);

    }

    catch (HttpRequestException e)

    {

        System.Diagnostics.Debug.WriteLine("\nException Caught!");

        System.Diagnostics.Debug.WriteLine("Message :{0} ", e.Message);

    }

    client.Dispose();

    return View(datalist);

   }

  }

 }

物种.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace diversity.Models

{

  public class Species

  {

   public int UserId { get; set; }

   public int Id { get; set; }

   public string Title { get; set; }

   public string Body { get; set; }

  }

}

物种详情.cshtml


 @model IEnumerable<diversity.Models.Species>


 @{

   ViewBag.Title = "SpeciesDetails";

  Layout = "~/Views/Shared/_Layout.cshtml";

 }


<h2>SpeciesDetails</h2>


<p>

   @Html.ActionLink("Create New", "Create")

</p>


 <table class="table">

<tr>

<th>

    @Html.DisplayNameFor(model => model.UserId)

</th>

<th>

    @Html.DisplayNameFor(model => model.Title)

</th>

<th>

    @Html.DisplayNameFor(model => model.Body)

</th>

<th></th>

</tr>



哔哔one
浏览 229回答 2
2回答

撒科打诨

将此用作您的模型类:&nbsp;using System.Collections.Generic;&nbsp; &nbsp; using Newtonsoft.Json;&nbsp; &nbsp; public partial class JsonModel&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("offset")]&nbsp; &nbsp; &nbsp; &nbsp; public long Offset { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("limit")]&nbsp; &nbsp; &nbsp; &nbsp; public long Limit { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("endOfRecords")]&nbsp; &nbsp; &nbsp; &nbsp; public bool EndOfRecords { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("count")]&nbsp; &nbsp; &nbsp; &nbsp; public long Count { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("results")]&nbsp; &nbsp; &nbsp; &nbsp; public List<Result> Results { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Result&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("key")]&nbsp; &nbsp; &nbsp; &nbsp; public long Key { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("datasetKey")]&nbsp; &nbsp; &nbsp; &nbsp; public string DatasetKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("nubKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long NubKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("parentKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long ParentKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("parent")]&nbsp; &nbsp; &nbsp; &nbsp; public string Parent { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("kingdom")]&nbsp; &nbsp; &nbsp; &nbsp; public string Kingdom { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("phylum")]&nbsp; &nbsp; &nbsp; &nbsp; public string Phylum { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("order")]&nbsp; &nbsp; &nbsp; &nbsp; public string Order { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("family")]&nbsp; &nbsp; &nbsp; &nbsp; public string Family { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("genus")]&nbsp; &nbsp; &nbsp; &nbsp; public string Genus { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("kingdomKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long KingdomKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("phylumKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long PhylumKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("classKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long ClassKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("orderKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long OrderKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("familyKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long FamilyKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("genusKey")]&nbsp; &nbsp; &nbsp; &nbsp; public long GenusKey { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("scientificName")]&nbsp; &nbsp; &nbsp; &nbsp; public string ScientificName { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("canonicalName")]&nbsp; &nbsp; &nbsp; &nbsp; public string CanonicalName { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("authorship")]&nbsp; &nbsp; &nbsp; &nbsp; public string Authorship { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("nameType")]&nbsp; &nbsp; &nbsp; &nbsp; public string NameType { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("taxonomicStatus")]&nbsp; &nbsp; &nbsp; &nbsp; public string TaxonomicStatus { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("rank")]&nbsp; &nbsp; &nbsp; &nbsp; public string Rank { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("origin")]&nbsp; &nbsp; &nbsp; &nbsp; public string Origin { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("numDescendants")]&nbsp; &nbsp; &nbsp; &nbsp; public long NumDescendants { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("numOccurrences")]&nbsp; &nbsp; &nbsp; &nbsp; public long NumOccurrences { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("habitats")]&nbsp; &nbsp; &nbsp; &nbsp; public List<object> Habitats { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("nomenclaturalStatus")]&nbsp; &nbsp; &nbsp; &nbsp; public List<object> NomenclaturalStatus { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("threatStatuses")]&nbsp; &nbsp; &nbsp; &nbsp; public List<object> ThreatStatuses { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("descriptions")]&nbsp; &nbsp; &nbsp; &nbsp; public List<object> Descriptions { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("vernacularNames")]&nbsp; &nbsp; &nbsp; &nbsp; public List<object> VernacularNames { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("higherClassificationMap")]&nbsp; &nbsp; &nbsp; &nbsp; public Dictionary<string, string> HigherClassificationMap { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("synonym")]&nbsp; &nbsp; &nbsp; &nbsp; public bool Synonym { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("class")]&nbsp; &nbsp; &nbsp; &nbsp; public string Class { get; set; }&nbsp; &nbsp; }然后在你的课堂上做这个&nbsp; &nbsp; &nbsp;var data = JsonConvert.DeserializeObject<JsonModel>("JsonData");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = data.Results;//Loop as appropriate, using index 0 for testing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string AuthorShip = result[0].Authorship;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string Origin = result[0].Origin;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string parent = result[0].Parent;
打开App,查看更多内容
随时随地看视频慕课网APP