I am trying to deserialize my jsonresult but I am getting an error message below:

"Cannot convert from System.Web.Mvc.Result to string"

Here's my jsonresult:

[{"description":"BANANA","quantity":"12","productPrice":"40000","discount":"","isTaxable":true,"finalPrice":"9999","lineItemTotal":"999","orderId":8},{"description":"APPLES","quantity":"20","productPrice":"860000","discount":"","isTaxable":false,"finalPrice":"12000","lineItemTotal":"10000","orderId":8}]

Here's my controller method:

 [System.Web.Mvc.HttpPost]public JsonResult CreateLineItems(JsonResult data){ResultDto dto = JsonConvert.DeserializeObject<ResultDto>(data);//for (int i = 0; i < data.Data; i++)//{// //}return data;}

Here's my ResultDto class:

public class ResultDto{public string description { get; set; }public string quantity { get; set; }public string productPrice { get; set; }public string discount { get; set; }public bool isTaxable { get; set; }public string finalPrice { get; set; }public string lineItemTotal { get; set; }public int orderId { get; set; }}

The JsonResut contains two objects (i.e, banana and apple). So, I want to loop through the Data and get two ResultDtos . How do I achieve this?

This line gives me the error: `ResultDto dto = JsonConvert.DeserializeObject(data);

I am sending the data like this from the view:

$.ajax({url: url,data: {data: JSON.stringify(lineitems)},cache: false,type: "POST",success: function (data) {},error: function (reponse) {alert("error : " + reponse);}});

What's the best way to get this data ( which is an array of objects) in my controller? Once I get this data in my controller, I want to persist it in the db. So, I am not sure if JsonResult is the correct return type for my method.

4

Best Answer


You already have a JSon. The way to fix your issue right now is to stringify your JSONObject, and then deserialize it to the type you want.

string json = JsonConvert.SerializeObject(jsonResult.Data);ResultDto dto = JsonConvert.DeserializeObject<ResultDto>(json);

I don't know how you managed to accept a JsonResult in your controller, but stop doing that. JsonResult is for returning data to the client, not to accept data from the client. Your controller should accept a ResultDto[], which should deserialize automatically from your JSON by the MVC framework. Then, you can continue working on it.

Also, as others noted in comments, isTaxable is bool and orderId is int, so type your ResultDto properties correctly.

Well it's because the JSONResult according to documentation : jsonResult is already formatted. You need to get into your data.Data object of jsonResult.

try replacing this:

ResultDto dto = JsonConvert.DeserializeObject<ResultDto>(data);

with this:

ResultDto dto = JsonConvert.DeserializeObject<ResultDto>(data.Data);

If this won't work try solution of Sakuto.

Change isTaxable to bool firstly

public class ResultDto{public string description { get; set; }public string quantity { get; set; }public string productPrice { get; set; }public string discount { get; set; }public bool isTaxable { get; set; }public string finalPrice { get; set; }public string lineItemTotal { get; set; }public int orderId { get; set; }}

Then, try to deserialize

var dtos = JsonConvert.DeserializeObject<IEnumerable<ResultDto>>(json);foreach(var dto in dtos){}