我有一个.NET 3.5项目。在ajax调用中,我创建了这样的json对象:
var jsonAOData = JSON.stringify(aoData);
...
data: "{jsonAOData : '" + jsonAOData + "'}"
...
然后我将它传递给WebMethod背后的代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string jQueryDataTable(string jsonAOData)
{
// Deserialize json into object
var customObj = JsonConvert.DeserializeObject<List<NameValue>>(jsonAOData);
我需要将对象反序列化为CustomParams对象。
这是我的对象:
[Serializable]
public class NameValue
{
public string Name { get; set; }
public string Value { get; set; }
}
[Serializable]
public class CustomParams
{
public string sEcho { get; set; }
public string iColumns { get; set; }
public string sColumns { get; set; }
public string iDisplayStart { get; set; }
public string iDisplayLength { get; set; }
public string mDataProp_0 { get; set; }
public string sSearch_0 { get; set; }
public string bRegex_0 { get; set; }
public string MyProperty { get; set; }
public string bSearchable_0 { get; set; }
public string bSortable_0 { get; set; }
public string mDataProp_1 { get; set; }
public string sSearch_1 { get; set; }
public string bRegex_1 { get; set; }
public string bSearchable_1 { get; set; }
public string bSortable_1 { get; set; }
public string mDataProp_2 { get; set; }
public string sSearch_2 { get; set; }
public string bRegex_2 { get; set; }
public string bSearchable_2 { get; set; }
public string bSortable_2 { get; set; }
public string mDataProp_3 { get; set; }
public string sSearch_3 { get; set; }
public string bRegex_3 { get; set; }
public string bSearchable_3 { get; set; }
public string bSortable_3 { get; set; }
public string mDataProp_4 { get; set; }
public string sSearch_4 { get; set; }
public string bRegex_4 { get; set; }
public string bSearchable_4 { get; set; }
public string bSortable_4 { get; set; }
public string mDataProp_5 { get; set; }
public string sSearch_5 { get; set; }
public string bRegex_5 { get; set; }
public string bSearchable_5 { get; set; }
public string bSortable_5 { get; set; }
public string mDataProp_6 { get; set; }
public string sSearch_6 { get; set; }
public string bRegex_6 { get; set; }
public string bSearchable_6 { get; set; }
public string bSortable_6 { get; set; }
public string mDataProp_7 { get; set; }
public string sSearch_7 { get; set; }
public string bRegex_7 { get; set; }
public string bSearchable_7 { get; set; }
public string bSortable_7 { get; set; }
public string sSearch { get; set; }
public string bRegex { get; set; }
public string iSortCol_0 { get; set; }
public string sSortDir_0 { get; set; }
public string iSortingCols { get; set; }
}
如果需要,我很乐意提供更多信息。感谢。
更新
我有所有名称,值的customObj 屏幕附上:
我还需要将所有数据传输到CustomParams。
这是传递给
的jsonAOData(只是一个字符串)后面的调试器代码的数据string jsonAOData
这是一个数据:
[{\"name\":\"sEcho\",\"value\":1},{\"name\":\"iColumns\",\"value\":8},{\"name\":\"sColumns\",\"value\":\",,,,,,,\"},{\"name\":\"iDisplayStart\",\"value\":0},{\"name\":\"iDisplayLength\",\"value\":10},{\"name\":\"mDataProp_0\",\"value\":0},{\"name\":\"sSearch_0\",\"value\":\"\"},{\"name\":\"bRegex_0\",\"value\":false},{\"name\":\"bSearchable_0\",\"value\":true},{\"name\":\"bSortable_0\",\"value\":true},{\"name\":\"mDataProp_1\",\"value\":1},{\"name\":\"sSearch_1\",\"value\":\"\"},{\"name\":\"bRegex_1\",\"value\":false},{\"name\":\"bSearchable_1\",\"value\":true},{\"name\":\"bSortable_1\",\"value\":true},{\"name\":\"mDataProp_2\",\"value\":2},{\"name\":\"sSearch_2\",\"value\":\"\"},{\"name\":\"bRegex_2\",\"value\":false},{\"name\":\"bSearchable_2\",\"value\":true},{\"name\":\"bSortable_2\",\"value\":true},{\"name\":\"mDataProp_3\",\"value\":3},{\"name\":\"sSearch_3\",\"value\":\"\"},{\"name\":\"bRegex_3\",\"value\":false},{\"name\":\"bSearchable_3\",\"value\":true},{\"name\":\"bSortable_3\",\"value\":true},{\"name\":\"mDataProp_4\",\"value\":4},{\"name\":\"sSearch_4\",\"value\":\"\"},{\"name\":\"bRegex_4\",\"value\":false},{\"name\":\"bSearchable_4\",\"value\":true},{\"name\":\"bSortable_4\",\"value\":true},{\"name\":\"mDataProp_5\",\"value\":5},{\"name\":\"sSearch_5\",\"value\":\"\"},{\"name\":\"bRegex_5\",\"value\":false},{\"name\":\"bSearchable_5\",\"value\":true},{\"name\":\"bSortable_5\",\"value\":true},{\"name\":\"mDataProp_6\",\"value\":6},{\"name\":\"sSearch_6\",\"value\":\"\"},{\"name\":\"bRegex_6\",\"value\":false},{\"name\":\"bSearchable_6\",\"value\":true},{\"name\":\"bSortable_6\",\"value\":true},{\"name\":\"mDataProp_7\",\"value\":7},{\"name\":\"sSearch_7\",\"value\":\"\"},{\"name\":\"bRegex_7\",\"value\":false},{\"name\":\"bSearchable_7\",\"value\":true},{\"name\":\"bSortable_7\",\"value\":true},{\"name\":\"sSearch\",\"value\":\"\"},{\"name\":\"bRegex\",\"value\":false},{\"name\":\"iSortCol_0\",\"value\":0},{\"name\":\"sSortDir_0\",\"value\":\"asc\"},{\"name\":\"iSortingCols\",\"value\":1}]"
我的最终代码如下:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string jQueryDataTable(string jsonAOData)
{
// Deserialize json into object
List<NameValue> jsonData = JsonConvert.DeserializeObject<List<NameValue>>(jsonAOData);
CustomParams paramObject = new CustomParams();
paramObject.sEcho = jsonData.Single(o=> o.Name == "sEcho").Value;
paramObject.iColumns = jsonData.Single(o=>o.Name == "iColumns").Value;
// the rest of the parameters
// ... code ...
}
答案 0 :(得分:0)
因为您的JSON是一个数组,所以它反序列化为一个数组/列表。您将需要手动映射到CustomObject类,使用反射,或使用AutoMapper之类的东西来自动执行某些反射。您可以实现自定义JSON反序列化器,但仍然需要使用反射为属性赋值。这是最简单的方法,没有反射:
var customObj = JsonConvert.DeserializeObject<List<NameValue>>(jsonAOData);
CustomParams paramObject = new CustomParams();
paramObject.sEcho = customObj.Single(o=>o.Name == 'sEcho').Value;
paramObject.iColumns = customObj.Single(o=>o.Name == 'iColumns ').Value;
// etc.
重新格式化JSON
如果您可以控制JSON格式,那么将格式化为平面对象而不是数组格式会更好:
{ "sEcho":"1","iColumns":8, "sColumns":5, ... }
然后可以将其直接反序列化为CustomParams对象:
CustomParams paramObject= JsonConvert.DeserializeObject<CustomParams>(jsonAOData);
您可以通过循环遍历数组中的每个项目来填充新的平面对象,然后通过javascript将原始JSON数组转换为平面对象:
flatObject[arrayItem.name] = arrayItem.value;