Deserialize JSON into Models that render in partial view (restsharp or any other method)

997 views Asked by At

I've never dealt with deserializing JSON and i'm just not getting it.

I call an API and get JSON returned, I then want to use the JSON data my partial view. My thought process is get JSON, deserialize into Model, pass model to view and away we go.

I've currently installed restsharp as reading through posts seemed the way to go. I'm completely open to other methods.

The 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"
}

}

Model - Used http://json2csharp.com/ recommended in other posts. Really cool in getting the models right.

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

Controller - Here is where is all goes a little pear shaped.

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

    }

The rendering into my partial view

    @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>
2

There are 2 answers

4
Dave Ward On BEST ANSWER

RestSharp can do this for you automatically:

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);
}
1
Hrvoje Hudo On

Looks like you're on the right track. Why no just:

if(OfferJ == null) 
    return PartialView(/* throw an exception, or whatever you want to do because de-serialization wasn't succesfull */)  

return PartialView("_pImageOfferModel", OfferJ);