为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
哔哔one 2021-08-22 15:48:49
我成功地从这个简单的 json 中得到了结果,并使用以下代码显示在视图中。物种控制器.csusing 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);   }  } }物种.csusing 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>
查看完整描述

2 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

将此用作您的模型类:


 using System.Collections.Generic;


    using Newtonsoft.Json;


    public partial class JsonModel

    {

        [JsonProperty("offset")]

        public long Offset { get; set; }


        [JsonProperty("limit")]

        public long Limit { get; set; }


        [JsonProperty("endOfRecords")]

        public bool EndOfRecords { get; set; }


        [JsonProperty("count")]

        public long Count { get; set; }


        [JsonProperty("results")]

        public List<Result> Results { get; set; }

    }


    public partial class Result

    {

        [JsonProperty("key")]

        public long Key { get; set; }


        [JsonProperty("datasetKey")]

        public string DatasetKey { get; set; }


        [JsonProperty("nubKey")]

        public long NubKey { get; set; }


        [JsonProperty("parentKey")]

        public long ParentKey { get; set; }


        [JsonProperty("parent")]

        public string Parent { get; set; }


        [JsonProperty("kingdom")]

        public string Kingdom { get; set; }


        [JsonProperty("phylum")]

        public string Phylum { get; set; }


        [JsonProperty("order")]

        public string Order { get; set; }


        [JsonProperty("family")]

        public string Family { get; set; }


        [JsonProperty("genus")]

        public string Genus { get; set; }


        [JsonProperty("kingdomKey")]

        public long KingdomKey { get; set; }


        [JsonProperty("phylumKey")]

        public long PhylumKey { get; set; }


        [JsonProperty("classKey")]

        public long ClassKey { get; set; }


        [JsonProperty("orderKey")]

        public long OrderKey { get; set; }


        [JsonProperty("familyKey")]

        public long FamilyKey { get; set; }


        [JsonProperty("genusKey")]

        public long GenusKey { get; set; }


        [JsonProperty("scientificName")]

        public string ScientificName { get; set; }


        [JsonProperty("canonicalName")]

        public string CanonicalName { get; set; }


        [JsonProperty("authorship")]

        public string Authorship { get; set; }


        [JsonProperty("nameType")]

        public string NameType { get; set; }


        [JsonProperty("taxonomicStatus")]

        public string TaxonomicStatus { get; set; }


        [JsonProperty("rank")]

        public string Rank { get; set; }


        [JsonProperty("origin")]

        public string Origin { get; set; }


        [JsonProperty("numDescendants")]

        public long NumDescendants { get; set; }


        [JsonProperty("numOccurrences")]

        public long NumOccurrences { get; set; }


        [JsonProperty("habitats")]

        public List<object> Habitats { get; set; }


        [JsonProperty("nomenclaturalStatus")]

        public List<object> NomenclaturalStatus { get; set; }


        [JsonProperty("threatStatuses")]

        public List<object> ThreatStatuses { get; set; }


        [JsonProperty("descriptions")]

        public List<object> Descriptions { get; set; }


        [JsonProperty("vernacularNames")]

        public List<object> VernacularNames { get; set; }


        [JsonProperty("higherClassificationMap")]

        public Dictionary<string, string> HigherClassificationMap { get; set; }


        [JsonProperty("synonym")]

        public bool Synonym { get; set; }


        [JsonProperty("class")]

        public string Class { get; set; }

    }

然后在你的课堂上做这个


     var data = JsonConvert.DeserializeObject<JsonModel>("JsonData");

                var result = data.Results;

//Loop as appropriate, using index 0 for testing

                string AuthorShip = result[0].Authorship;

                string Origin = result[0].Origin;

                string parent = result[0].Parent;


查看完整回答
反对 回复 2021-08-22
  • 2 回答
  • 0 关注
  • 228 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信