Answer the question
In order to leave comments, you need to log in
C# type casting?
At a stage of processing of a type (view) the following error takes off.
Help me fix it please.
The model element passed to the dictionary is of type "www.Models.API.StatTrafficSummary", but this dictionary requires a model element of type "System.Collections.Generic.IEnumerable`1[www.Models.API.StatTrafficSummary]".
public class StatTrafficSummary
{
public string date2 { get; set; }
public Min min { get; set; }
public Max max { get; set; }
public List<Datum> data { get; set; }
public int rows { get; set; }
public string date1 { get; set; }
public string id { get; set; }
public List<object> goals { get; set; }
public Totals totals { get; set; }
}
public class Min
{
public int denial { get; set; }
public int visits { get; set; }
public double new_visitors_perc { get; set; }
public int page_views { get; set; }
public int visit_time { get; set; }
public double depth { get; set; }
public int new_visitors { get; set; }
public int visitors { get; set; }
}
....
public ActionResult Index()
{
var client = new WebClient();
client.Headers.Add("User-Agent", "Nobody"); //my endpoint needs this...
var response = client.DownloadString(new Uri("http://api-metrika.yandex.ru/stat/traffic/summary.json?id=6673&pretty=1&oauth_token=ad821b834d934c"));
var j = JsonConvert.DeserializeObject<StatTrafficSummary>(response);
return View(j);
}
@model IEnumerable<zinbo.Models.API.Ya.StatTrafficSummary>
@{
ViewBag.Title = "Home Page";
}
@foreach (var item in Model)
{
<ul>
<li>@@Html.DisplayFor(modelItem => item.data[0].new_visitors)</li>
</ul>
}
Answer the question
In order to leave comments, you need to log in
Here is the json
{
"date2" : "20140414",
"min" : {
"denial" : 0,
"visits" : 2,
"new_visitors_perc" : 0.7857,
"page_views" : 14,
"visit_time" : 30,
"depth" : 1.6154,
"new_visitors" : 2,
"visitors" : 2
},
"max" : {
"denial" : 0.3125,
"visits" : 19,
"new_visitors_perc" : 1,
"page_views" : 50,
"visit_time" : 354,
"depth" : 7,
"new_visitors" : 17,
"visitors" : 19
},
"data" : [
{
"wday" : 6,
"denial" : 0,
"visits" : 2,
"new_visitors_perc" : 1,
"page_views" : 14,
"date" : "20140413",
"visit_time" : 354,
"depth" : 7,
"new_visitors" : 2,
"visitors" : 2,
"id" : "20140413"
},
{
"wday" : 5,
"denial" : 0.2308,
"visits" : 13,
"new_visitors_perc" : 0.8333,
"page_views" : 21,
"date" : "20140412",
"visit_time" : 124,
"depth" : 1.6154,
"new_visitors" : 10,
"visitors" : 12,
"id" : "20140412"
},
{
"wday" : 4,
"denial" : 0.3125,
"visits" : 16,
"new_visitors_perc" : 1,
"page_views" : 36,
"date" : "20140411",
"visit_time" : 211,
"depth" : 2.25,
"new_visitors" : 16,
"visitors" : 16,
"id" : "20140411"
},
{
"wday" : 3,
"denial" : 0.0526,
"visits" : 19,
"new_visitors_perc" : 0.8947,
"page_views" : 50,
"date" : "20140410",
"visit_time" : 84,
"depth" : 2.6316,
"new_visitors" : 17,
"visitors" : 19,
"id" : "20140410"
},
{
"wday" : 2,
"denial" : 0.2308,
"visits" : 13,
"new_visitors_perc" : 0.8462,
"page_views" : 22,
"date" : "20140409",
"visit_time" : 31,
"depth" : 1.6923,
"new_visitors" : 11,
"visitors" : 13,
"id" : "20140409"
},
{
"wday" : 1,
"denial" : 0.2857,
"visits" : 14,
"new_visitors_perc" : 0.7857,
"page_views" : 30,
"date" : "20140408",
"visit_time" : 30,
"depth" : 2.1429,
"new_visitors" : 11,
"visitors" : 14,
"id" : "20140408"
}
],
"rows" : 6,
"date1" : "20140408",
"id" : "6673690",
"goals" : [],
"totals" : {
"denial" : 0.2078,
"visits" : 77,
"new_visitors_perc" : 0.9178,
"page_views" : 173,
"visit_time" : 105,
"depth" : 2.2468,
"new_visitors" : 67,
"visitors" : 73
}
}
public class Min
{
public int denial { get; set; }
public int visits { get; set; }
public double new_visitors_perc { get; set; }
public int page_views { get; set; }
public int visit_time { get; set; }
public double depth { get; set; }
public int new_visitors { get; set; }
public int visitors { get; set; }
}
public class Max
{
public double denial { get; set; }
public int visits { get; set; }
public int new_visitors_perc { get; set; }
public int page_views { get; set; }
public int visit_time { get; set; }
public int depth { get; set; }
public int new_visitors { get; set; }
public int visitors { get; set; }
}
public class Datum
{
public int wday { get; set; }
public double denial { get; set; }
public int visits { get; set; }
public double new_visitors_perc { get; set; }
public int page_views { get; set; }
public string date { get; set; }
public int visit_time { get; set; }
public double depth { get; set; }
public int new_visitors { get; set; }
public int visitors { get; set; }
public string id { get; set; }
}
public class Totals
{
public double denial { get; set; }
public int visits { get; set; }
public double new_visitors_perc { get; set; }
public int page_views { get; set; }
public int visit_time { get; set; }
public double depth { get; set; }
public int new_visitors { get; set; }
public int visitors { get; set; }
}
public class StatTrafficSummary
{
public string date2 { get; set; }
public Min min { get; set; }
public Max max { get; set; }
public List<Datum> data { get; set; }
public int rows { get; set; }
public string date1 { get; set; }
public string id { get; set; }
public List<object> goals { get; set; }
public Totals totals { get; set; }
}
замените var j = JsonConvert.DeserializeObject<StatTrafficSummary>(response); на
var j = JsonConvert.DeserializeObject<StatTrafficSummary[]>(response);
@LChaos got a different error now
Исключение типа "Newtonsoft.Json.JsonSerializationException" возникло в Newtonsoft.Json.dll, но не было обработано в коде пользователя
Дополнительные сведения: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'www.Models.API.Ya.StatTrafficSummary[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'date2', line 2, position 13.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question