JSON反序列化
JSON反序列化
using System.Web.Script.Serialization;
/// <summary>
/// 找出EmpSalaryFix薪資
/// </summary>
/// <param name="EmpID"></param>
/// <param name="PayCode"></param>
private string SelSalary(string EmpID, string SalaryCode)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TrustERPConnectionString"].ConnectionString);
conn.Open();
string returnStr = "";
string strSQLSel = "SELECT " +
" A.EmpID, A.sDate, A.Amount, A.MonthTax, A.SalaryCode " +
"FROM dbo.EmpSalaryFix AS A INNER JOIN " +
"(SELECT SalaryCode, MAX(sDate) AS maxsDate " +
"FROM dbo.EmpSalaryFix " +
"WHERE (EmpID = '" + EmpID.Trim() + "') " +
"GROUP BY SalaryCode) AS idview ON A.SalaryCode = idview.SalaryCode AND A.sDate = idview.maxsDate " +
"WHERE (A.EmpID = '" + EmpID.Trim() + "') " +
"AND (A.SalaryCode = '" + SalaryCode.Trim() + "')";
DataTable dtSel = db.queryDataTable(strSQLSel);
conn.Close();
conn.Dispose();
return DataTableToJsonWithJavaScriptSerializer(dtSel);
}
public string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
程式中查詢
string retS00 = SelSalary("U00012", "0101000S00");
或
using Newtonsoft.Json;
//JSON反序列化
string jsonText = SelSalary(strEmpID.Trim(), "0101000S01");
JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);
string Amount = jo["Amount"].ToString();
string MonthTax = jo["MonthTax"].ToString();