Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken. Error getting when I pass the json

Tags:

json

c#

json.net

I need to pass the checked check-boxes code to C# from JavaScript. I was able to send the code via JSON. And my JSON value comes as a JArray. And I am getting the exception in the title.

JSON:

{
  "Items": [
    "100066",
    "100067"
  ]
}

C#:

public ActionResult UpdateTransportRequests()       
{
    string json;
    using (var reader = new StreamReader(Request.InputStream))
    {
        json = reader.ReadToEnd();
    }

    JObject jo = (JObject)JsonConvert.DeserializeObject(json);

    string lineItems = jo.Value<string>("Items");

    RequestDataAccess rda = new RequestDataAccess();
    decimal reqId = decimal.Parse(lineItems);
    rda.ApproveReject_Request(reqId, "A", "");

    return Json(new { result = "success" });
}

Client Side:

function approveAll(requestid) {
    var items = [];

    $('#grid tbody').find('input:checkbox:checked').each(function (index, item) {
        var rowIndex = $(this).closest('tr').index();
        items.push($('#grid tbody').find('tr:eq(' + rowIndex + ')').find('td:eq(1)').text().replace('TR-', ''));

    });
    $.ajax({
        type: "POST",
        url: '@Url.Action("UpdateTransportRequestsAll", "TransportRequest")',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ "Items": items }),
        success: function (response) {
            alert(response.result);
        },
        dataType: "json"
    });
}

Please help me to fix this error.

like image 591
Sinthuja.Chandra Avatar asked Mar 05 '14 12:03

Sinthuja.Chandra


3 Answers

It can be converted from JArray to List as array.ToObject<List<TargetDataType>>();

like image 69
SharK Avatar answered Nov 20 '22 13:11

SharK


In this situation, I don't think JsonConvert.DeserializeObject is the appropriate method to use. The error about converting from JArray to JToken likely occurs because DeserializeObject is attempting to directly deserialize to JObject, which will fail because it won't find the required fields on JObject.

Try using JObject.Parse(json) to get a dynamic object. You can use it like this afterwards:

dynamic jo = JObject.Parse(json);
var items = jo.Items;
foreach(var item in items)
{
    string lineItem = (string) item;
    decimal reqId = decimal.Parse(lineItem);

    // Use as required
}
like image 22
Chris Mantle Avatar answered Nov 20 '22 13:11

Chris Mantle


public ActionResult UpdateTransportRequests() 
{
    string json;

    using (var reader = new StreamReader(Request.InputStream))
    {
        json = reader.ReadToEnd();
    }

    dynamic jo = JObject.Parse(json);
    foreach (var item in jo.Items)
    {
        decimal reqId = (decimal)item;
        RequestDataAccess rda = new RequestDataAccess();
        rda.AllApproveReject_Request(reqId, "A", "");
    }
    return Json(new { result = "success" });
}
like image 1
Sinthuja.Chandra Avatar answered Nov 20 '22 13:11

Sinthuja.Chandra