I am attempting to convert a Jsonstring into a c# listObject. The data is being sent from JavaScript using the following code:
params = "data=" + JSON.stringify(queryResult.$$rows);
XHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XHR.setRequestHeader("Content-length", params.length);
XHR.setRequestHeader("Connection", "close");
XHR.send(params);
Then in ASP.NET, I am trying to deserialize the Jsonstring with:
public class HomeController : Controller
{
[HttpPost]
public ActionResult doWork(string data)
{
var dump = JsonConvert.DeserializeObject<List<RootObject>>(data);
return new EmptyResult();
}
}
public class RootObject
{
public string data { get; set; }
public string text { get; set; }
}
Upon inspecting the local variable data, I can see a valid json string:
[
[
{
"data":"Australia",
"text":"Australia"
}
],
[
{
"data":"China",
"text":"China"
}
],
[
{
"data":"Hong Kong",
"text":"Hong Kong"
}
],
[
{
"data":"Indonesia",
"text":"Indonesia"
}
],
[
{
"data":"Netherlands",
"text":"Netherlands"
}
]
]
However, when ASP.NET attempts to execute JsonConvert.DeserializeObject>(data); it results in an error message:
JsonSerializationException was unhandled by user code an exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'maps.Controllers.RootObject' because the type requires a JSON object (e.g. {"name" : "value"}) to deserialize correctly.
How can I resolve this issue? And is this the correct approach in JavaScript?