实体:
[Serializable()]
public class povgloablDTO
{
public string key { get; set; }
public string gloable { get; set; }
}
var arr1 = [];
for (var i = 0; i < parstore.getCount(); i++) {
arr1.push({ key: parstore.getAt(i).data.key, gloable: parstore.getAt(i).data.gloable });
}
Ext.Ajax.request({
url: '@Url.Action("SumbitAdapterSetting", "AdapterSetting")' + location.search,
method: 'post',
params: { povgloabl: Ext.JSON.encode(arr1)},
callback: function (options, success, response) {
})
Controller:
List<povgloablDTO> globaldto = JsonHelper.FromJson<List<povgloablDTO>>(povgloabl);//app_Parame中的关键
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data;
public static class JsonHelper
{
private static JsonSerializerSettings _jsonSettings;
static JsonHelper()
{
IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter();
datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
_jsonSettings = new JsonSerializerSettings();
_jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
_jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
_jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
_jsonSettings.Converters.Add(datetimeConverter);
}
/// <summary>
/// 将指定的对象序列化成 JSON 数据。
/// </summary>
/// <param name="obj">要序列化的对象。</param>
/// <returns></returns>
public static string ToJson(this object obj)
{
try
{
if (null == obj)
return null;
return JsonConvert.SerializeObject(obj, Formatting.None, _jsonSettings);
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 将指定的对象序列化成string
/// 转换后格式为:{headers:[],rows:[]}
/// </summary>
/// <param name="dt">要序列化的对象。</param>
/// <returns></returns>
public static string ToJson(this DataTable dt)
{
try
{
if (null == dt)
return null;
var headers = new List<string>() ;
foreach (DataColumn col in dt.Columns)
{
headers.Add(col.Caption);
}
StringBuilder Json = new StringBuilder();
Json.Append("{");
Json.Append("\"headers\":");
Json.Append(headers.ToJson());
Json.Append(",\"rows\":[");
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Json.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
if (j < dt.Columns.Count - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < dt.Rows.Count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}");
return Json.ToString();
}
catch
{
return null;
}
}
/// <summary>
/// 将指定的 JSON 数据反序列化成指定对象。
/// </summary>
/// <typeparam name="T">对象类型。</typeparam>
/// <param name="json">JSON 数据。</param>
/// <returns></returns>
public static T FromJson<T>(this string json)
{
try
{
return JsonConvert.DeserializeObject<T>(json, _jsonSettings);
}
catch (Exception ex)
{
return default(T);
}
}
}