Parsing null values

Jan 25, 2012 at 11:04 PM

How do you parse nulls using JSON when it deserializes to a defined C# class type? Every time, the parses throws an exception. It can't seem to handle nulls but they seem to be common in JSON data

Coordinator
Jan 27, 2012 at 1:58 PM

Thank you for reporting this bug. The new Json Toolkit v2.1 can now deserialize data with null values. A few test cases related to null values are also added to the Visual Studio test project.

May 6, 2015 at 6:08 PM
Edited May 6, 2015 at 6:12 PM
I moved this to new thread "Returning JsonArray from MVC Action". You can delete this one.

In Json Toolkit v 4.3.8.333, I'm building an array of JsonObject like this:
        JsonArray joArray = new JsonArray();
        foreach (var row in lstRows)
        {
          JsonObject jrow = new JsonObject();
          foreach (var cell in row.RowAttributes)
          {
            string id = cell.ListColumns.ColumnName.Replace(" ", "");
            if (id == "Item#")
            {
              id = "id";
            }
            if (id != "id")
            {
              string val = cell.CellValue.Replace("\"", "\\\"");
              jrow.Add(id, val);
            }
            else
            {
              int iVal = Int32.Parse(cell.CellValue); //e.g. 1
              jrow.Add(id, iVal);
            }
          }
          jrow.Add("cmdButton", "Edit");
          joArray.Add(jrow);
        }
This gives me the SlickGrid table data i need to return.
If I try to return this as JsonResult from my MVC Action, the Ajax callback sees the array element 0 as Key: "id", Value: 1 instead of { "id": 1 }.
I'm using this line to return the JsonResult data: return Json(joArray, JsonRequestBehavior.AllowGet).

As a workaround, I changed the Action return value to "string" and return joArray.ToString() instead. This works in my Ajax callback, but I was wondering if it is the best way of doing this or is there a better way?