将 JSON 反序列化为在局部视图中呈现的模型(restsharp 或任何其他方法)
Deserialize JSON into Models that render in partial view (restsharp or any other method)
我从来没有处理过反序列化 JSON,我就是不明白。
我调用 API 并返回 JSON,然后我想使用 JSON 数据作为我的部分视图。我的思考过程是获取 JSON,反序列化为模型,将模型传递给视图,然后我们开始。
我目前已经安装了 restsharp,因为阅读帖子似乎是可行的方法。我对其他方法完全开放。
JSON
{
"Offer": [
{
"Oid": 3,
"Type": 1,
"Name": "New Offer 3",
"Headline": "New Offer HL",
"Content": "New Offer BL",
"Terms": "New Offer Terms",
"DisplayOrder": 2,
"TZone": 1,
"ExpiryDays": 31,
"OfferLandingUrl": null,
"OfferImageM": null,
"OfferImageD": null,
"OfferVideo": null,
"MaxShares": "10"
}
],
"OfferUrls": {
"Url1": "http://localhost:49574/api/Offer/OfferDirect?oid=3",
"Url2": "http://localhost:49574/api/Offer/OfferDirect?oid=7",
"Url3": "http://localhost:49574/api/Offer/OfferDirect?oid=7"
}
}
型号 - 使用 http://json2csharp.com/ 在其他帖子中推荐的型号。正确设置模型真的很酷。
public class Offer
{
public int Oid { get; set; }
public int Type { get; set; }
public string Name { get; set; }
public string Headline { get; set; }
public string Content { get; set; }
public string Terms { get; set; }
public int DisplayOrder { get; set; }
public int TZone { get; set; }
public int ExpiryDays { get; set; }
public string OfferLandingUrl { get; set; }
public string OfferImageM { get; set; }
public string OfferImageD { get; set; }
public string OfferVideo { get; set; }
public string MaxShares { get; set; }
}
public class OfferUrls
{
public string Url1 { get; set; }
public string Url2 { get; set; }
public string Url3 { get; set; }
}
public class RootObject
{
public List<Offer> Offer { get; set; }
public OfferUrls OfferUrls { get; set; }
}
控制器 - 这是所有东西都呈梨形的地方。
public ActionResult GetImageOffer(string oid, string otr)
{
var client = new RestClient("valid url");
var request = new RestRequest("/Offer/OfferDirect", Method.POST);
request.AddQueryParameter("oid", oid);
request.AddQueryParameter("otr", otr);
request.AddHeader("apikey", "secretkeyhere");
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
// trying this but get stuck/lost what do do next
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
//The works but this i get stuck on this
RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();
var OfferJ = deserial.Deserialize<List<OfferJSON>>(response);
//OfferJSON OfferJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<OfferJSON>>(response.Content);
return PartialView("_pImageOfferModel", //insert right here//);
}
渲染到我的局部视图
@using Demo.Models
@model Demo.Models.RootObject
<div>
//I can't seem to access the next level of data. being @Model.Offer.Headline
<p>@Model.Offer</p>
<p>@Model.OfferUrls</p>
</div>
看来您的方向是正确的。为什么不只是:
if(OfferJ == null)
return PartialView(/* throw an exception, or whatever you want to do because de-serialization wasn't succesfull */)
return PartialView("_pImageOfferModel", OfferJ);
RestSharp 可以自动为您执行此操作:
public ActionResult GetImageOffer(string oid, string otr)
{
var client = new RestClient("valid url");
var request = new RestRequest("/Offer/OfferDirect", Method.POST);
request.AddQueryParameter("oid", oid);
request.AddQueryParameter("otr", otr);
request.AddHeader("apikey", "secretkeyhere");
request.RequestFormat = DataFormat.Json;
RestResponse<RootObject> response = client.Execute<RootObject>(request);
return PartialView("_pImageOfferModel", response.Data);
}
我从来没有处理过反序列化 JSON,我就是不明白。
我调用 API 并返回 JSON,然后我想使用 JSON 数据作为我的部分视图。我的思考过程是获取 JSON,反序列化为模型,将模型传递给视图,然后我们开始。
我目前已经安装了 restsharp,因为阅读帖子似乎是可行的方法。我对其他方法完全开放。
JSON
{
"Offer": [
{
"Oid": 3,
"Type": 1,
"Name": "New Offer 3",
"Headline": "New Offer HL",
"Content": "New Offer BL",
"Terms": "New Offer Terms",
"DisplayOrder": 2,
"TZone": 1,
"ExpiryDays": 31,
"OfferLandingUrl": null,
"OfferImageM": null,
"OfferImageD": null,
"OfferVideo": null,
"MaxShares": "10"
}
],
"OfferUrls": {
"Url1": "http://localhost:49574/api/Offer/OfferDirect?oid=3",
"Url2": "http://localhost:49574/api/Offer/OfferDirect?oid=7",
"Url3": "http://localhost:49574/api/Offer/OfferDirect?oid=7"
}
}
型号 - 使用 http://json2csharp.com/ 在其他帖子中推荐的型号。正确设置模型真的很酷。
public class Offer
{
public int Oid { get; set; }
public int Type { get; set; }
public string Name { get; set; }
public string Headline { get; set; }
public string Content { get; set; }
public string Terms { get; set; }
public int DisplayOrder { get; set; }
public int TZone { get; set; }
public int ExpiryDays { get; set; }
public string OfferLandingUrl { get; set; }
public string OfferImageM { get; set; }
public string OfferImageD { get; set; }
public string OfferVideo { get; set; }
public string MaxShares { get; set; }
}
public class OfferUrls
{
public string Url1 { get; set; }
public string Url2 { get; set; }
public string Url3 { get; set; }
}
public class RootObject
{
public List<Offer> Offer { get; set; }
public OfferUrls OfferUrls { get; set; }
}
控制器 - 这是所有东西都呈梨形的地方。
public ActionResult GetImageOffer(string oid, string otr)
{
var client = new RestClient("valid url");
var request = new RestRequest("/Offer/OfferDirect", Method.POST);
request.AddQueryParameter("oid", oid);
request.AddQueryParameter("otr", otr);
request.AddHeader("apikey", "secretkeyhere");
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
// trying this but get stuck/lost what do do next
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
//The works but this i get stuck on this
RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();
var OfferJ = deserial.Deserialize<List<OfferJSON>>(response);
//OfferJSON OfferJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<OfferJSON>>(response.Content);
return PartialView("_pImageOfferModel", //insert right here//);
}
渲染到我的局部视图
@using Demo.Models
@model Demo.Models.RootObject
<div>
//I can't seem to access the next level of data. being @Model.Offer.Headline
<p>@Model.Offer</p>
<p>@Model.OfferUrls</p>
</div>
看来您的方向是正确的。为什么不只是:
if(OfferJ == null)
return PartialView(/* throw an exception, or whatever you want to do because de-serialization wasn't succesfull */)
return PartialView("_pImageOfferModel", OfferJ);
RestSharp 可以自动为您执行此操作:
public ActionResult GetImageOffer(string oid, string otr)
{
var client = new RestClient("valid url");
var request = new RestRequest("/Offer/OfferDirect", Method.POST);
request.AddQueryParameter("oid", oid);
request.AddQueryParameter("otr", otr);
request.AddHeader("apikey", "secretkeyhere");
request.RequestFormat = DataFormat.Json;
RestResponse<RootObject> response = client.Execute<RootObject>(request);
return PartialView("_pImageOfferModel", response.Data);
}