0

Help me find the error, why does not it work? I was guided by this (https://www.newtonsoft.com/json/help/html/DeserializeWithLinq.htm) documentation.

This is my controller:

    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);
        }
    }

Classes 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; }
    }
}

View 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>

Shows here this error "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1."
Help me please.

Pavel Fedorov
  • 153
  • 1
  • 2
  • 9
  • 2
    Look at the JSON. It isn't an array. Look carefully at the JSON in your documentation. It *is* an array. –  Aug 26 '17 at 23:54
  • Help fix, I do not know what needs to be changed, the error still remains the same. – Pavel Fedorov Aug 27 '17 at 01:37
  • 1
    Well, let's think about this. You are using `JArray` to parse a JSON *object*. Maybe what you need to use is `JObject`? –  Aug 27 '17 at 02:30

1 Answers1

0

looking at the JSON string it is not an array, it is JSON object, which contains few arrays, looking at your class, you want 4 properties that are coming from the data object, which is not Array, mean you only have one value for these properties.

a better way to do this is by creating your own classes and do something like this, but for all properties. I think you are trying it this way, but you should include all properties, status, data, and data should be another class which will contain all the properties, see the data you are receiving in the image Your Object

another way to do it deserializes it to dynamic object and assign the values accordingly, something like this

var json = webClient.DownloadString(@"http://www.json-generator.com/api/json/get/ckJzNVJRIO?indent=2");
dynamic TheJsonObj = JObject.Parse(json);
flypool  theData = new flypool{
    hashRate = TheJsonObj.data.hasRate,
        blocksPerHour = TheJsonObj.data.blocksPerHour,
        priceUsd  = TheJsonObj.data.priceUsd  ,
        priceBtc= TheJsonObj.data.priceBtc
};
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Munzer
  • 2,216
  • 2
  • 18
  • 25