I am having trouble in converting a JSON string to a C# object with Json.NET.
I used the AJAX call .ashx
$.ajax({
url: "/Handler/Handler.ashx?WorkType=SaveData",
type: "POST",
data: JSON.stringify({ 'DataInfo': Info }),
//data: "{'DataInfo':" + JSON.stringify(Info) + "}",
dataType: "json",
success: function (data, textStatus, jqXHR) {
if (data.Result) {
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
}
});
And my Json String is like
{
"DataInfo": [
{
"EditType": "Create",
"CustCode": "SSG",
"KeyNoStr": "rgrg",
"Requester": "rgrg",
"VerificationCode": "VAVBZ",
"Databody": "TESt123",
"HasMap": false,
"IsColse": false,
"HasOrder": false,
"IsUrgent": true
}
]
}
and my .ashx Server side code is like..
public class DataInfo
{
public string EditType { get; set; }
public string CustCode { get; set; }
public string KeyNoStr { get; set; }
public string Requester { get; set; }
public string VerificationCode { get; set; }
public string Databody { get; set; }
public string HasMap { get; set; }
public string IsColse { get; set; }
public string HasOrder { get; set; }
public string IsUrgent { get; set; }
}
main function in .ashx
public void ProcessRequest(HttpContext context)
{
Request = context.Request;
Response = context.Response;
string lv_strResult = string.Empty;
DataInfo lv_oInfo = JsonConvert.DeserializeObject<DataInfo >((new StreamReader(Request.InputStream)).ReadToEnd());
}
What I am doing wrong?
That's because that JSON object is an Array of DataInfo
, so you need to deserialize using these classes
public class DataInfo
{
public string EditType { get; set; }
public string CustCode { get; set; }
public string KeyNoStr { get; set; }
public string Requester { get; set; }
public string VerificationCode { get; set; }
public string Databody { get; set; }
public bool HasMap { get; set; }
public bool IsColse { get; set; }
public bool HasOrder { get; set; }
public bool IsUrgent { get; set; }
}
public class RootObject
{
public List<DataInfo> DataInfo { get; set; }
}
//your method
public void ProcessRequest(HttpContext context)
{
Request = context.Request;
Response = context.Response;
string lv_strResult = string.Empty;
DataInfo lv_oInfo = JsonConvert.DeserializeObject<RootObject>((new StreamReader(Request.InputStream)).ReadToEnd());
}
Your JSON represents structure with one property named DataInfo
of type List<DataInfo>
So you need to replace DataInfo lv_oInfo = JsonConvert.DeserializeObject<DataInfo >((new StreamReader(Request.InputStream)).ReadToEnd());
with
var jobject = (JObject)JsonConvert.DeserializeObject((new StreamReader(Request.InputStream)).ReadToEnd());
var di_json = jobject["DataInfo"].ToString(); // Dirty but this is the main idea
var data_infos = JsonConvert.DeserializeObject<List<DataInfo>>(di_json);
This is not the best code in the world because of double deserialization, but it shows the idea.
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.