1

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?

Rob
27.4k16 gold badges89 silver badges103 bronze badges
asked Aug 27, 2015 at 3:06
1
  • Try just getting the string response first and see if its empty. Commented Aug 27, 2015 at 3:10

2 Answers 2

2

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());
 }
answered Aug 27, 2015 at 3:11

1 Comment

Hi @Kevin Chang Have you tested the solution?
0

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.

answered Aug 27, 2015 at 3:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.