I am trying to deserialize a Jsonstring to c# listObject. The data is coming from javascript with:
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 try 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; }
}
If i'm looking in the local variabel data. I found a valid jsong string:
[
[
{
"data":"Australia",
"text":"Australia"
}
],
[
{
"data":"China",
"text":"China"
}
],
[
{
"data":"Hong Kong",
"text":"Hong Kong"
}
],
[
{
"data":"Indonesia",
"text":"Indonesia"
}
],
[
{
"data":"Netherlands",
"text":"Netherlands"
}
]
]
When asp.net is trying to execute JsonConvert.DeserializeObject>(data); it wil return a error message:
JsonSerializationException was unhandled by user code an exception of type 'Newtonsoft.Json.JsonSerializationException' occred in newTonisoft.Json.ddl but was not handled in user code
Additional information: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'maps.Controllers.RootObject' because the type requires a JSON object (eg {"name" : "value"}) to deserialize correctly.
How do i fix this? and is this the right javascript way?
string jsonTxt = @"[
[
{
""data"":""Australia"",
""text"":""Australia""
}
],
[
{
""data"":""China"",
""text"":""China""
}
],
[
{
""data"":""Hong Kong"",
""text"":""Hong Kong""
}
],
[
{
""data"":""Indonesia"",
""text"":""Indonesia""
}
],
[
{
""data"":""Netherlands"",
""text"":""Netherlands""
}
]
]";
var result = JsonConvert.DeserializeObject<List<RootObject>[]>(jsonTxt);
The result is your need
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.