【发布时间】:2017-08-26 23:48:56
【问题描述】:
帮我找出错误,为什么它不起作用? 我以这个 (https://www.newtonsoft.com/json/help/html/DeserializeWithLinq.htm) 文档为指导。
这是我的控制器:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
// Example JSON
var webClient = new WebClient();
var json = webClient.DownloadString(@"http://www.json-generator.com/api/json/get/ckJzNVJRIO?indent=2");
JArray flypoolArray = JArray.Parse(json);
IList<flypool> blogPosts = flypoolArray.Select(p => new flypool
{
hashRate = (string)p["data"]["hashRate"],
blocksPerHour = (string)p["data"]["blocksPerHour"],
priceUsd = (string)p["data"]["priceUsd"],
priceBtc = (string)p["data"]["priceBtc"],
}).ToList();
return View(blogPosts);
}
}
类 flypool.cs
namespace WebApplicationParse.Models
{
public class flypool
{
public string hashRate { get; set; }
public string blocksPerHour { get; set; }
public string priceUsd { get; set; }
public string priceBtc { get; set; }
}
}
查看Index.cshtml
@model WebApplicationParse.Models.flypool
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<ul>
<li>@Model.hashRate</li>
</ul>
</div>
</div>
在此处显示此错误"Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1."
请帮帮我。
【问题讨论】:
-
查看 JSON。它不是一个数组。仔细查看文档中的 JSON。它是一个数组。
-
帮忙修复,不知道要改什么,还是报错。
-
好吧,让我们考虑一下。您正在使用
JArray解析 JSON object。也许您需要使用的是JObject?
标签: c# json asp.net-mvc parsing