0

I want to deserialize some JSON data and pass it to view in my ASP.NET MVC 5 app.

My model:

public class OrderModel
{
    public string Seat { get; set; }
    public string objectId { get; set; }
    public DateTime? createdAt { get; set; }

}

I've read that to deserialize JSON data to list of object i have to make "root" class which is here:

public class OrderRootModel
{
    public List<OrderModel> OrderList { get; set; }

}

Here is a method that gets JSON data in ApiModel class:

//ApiModel class
    public OrderRootModel GetOrderData()
        {

            string url = "https://api.parse.com/1/classes/Orders";

            OrderRootModel model = new OrderRootModel();
            model = JsonConvert.DeserializeObject<OrderRootModel>(getParseIdData(url));
            return model;
        }

My controller:

public ActionResult Index()
        {
            ApiModel model = new ApiModel();

            return View(model.GetOrderData()) ;
        }

My view(for sake of testing it's pretty much an empty page):

@model myCinema.Models.OrderRootModel
@{
    ViewBag.Title = "Index";
    Layout = null;
}

<h2>Order list</h2>

@foreach (var item in Model.OrderList)
{
    @item.objectId;
    @item.createdAt;
    @item.Seat
}

Now after launching my app i get " Object reference not set to an instance of an object." on "@foreach (var item in Model.OrderList)" line. Can someone help me to solve this problem?

EDIT: To answer why my question is different than others - i understand what usually causes this exception, but i've clearly made a mistake in my code which i cannot find, therefore i'm asking you guys to help me find it. Also my List is inside root class

I will provide rest of the code and JSON i recieve soon.

EDIT2: So in my GetOrderData on return line model appears to be null.

Here is my api JSON string respose from getParseIdData method:

{"results":[{"Seat":"5","createdAt":"2015-11-29T18:50:15.320Z","objectId":"BsDSolYPsT","updatedAt":"2015-11-29T19:40:55.020Z"},{"Seat":"6","createdAt":"2015-12-02T22:31:36.892Z","objectId":"kQJ0R5TUvw","updatedAt":"2015-12-02T22:31:36.892Z"},{"Seat":"7","createdAt":"2015-12-02T22:31:40.261Z","objectId":"sVtdj3aipb","updatedAt":"2015-12-02T22:31:40.261Z"},{"Seat":"8","createdAt":"2015-12-02T22:31:43.082Z","objectId":"7oH2ySrDFH","updatedAt":"2015-12-02T22:31:43.082Z"}]}

Here is method you asked for

public string getParseIdData(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers.Add("X-Parse-Application-Id", id);
        request.Headers.Add("X-Parse-REST-API-KEY", key);
        try
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                String errorText = reader.ReadToEnd();
                Console.WriteLine(errorText);
            }
            throw;
        }
    }
Kesto
  • 545
  • 1
  • 6
  • 15

0 Answers0