ASP. NET Conversion of JSON String and Entity Class
- 2021-08-03 09:59:38
- OfStack
For more information about JSON, please find out about google by yourself! If you want me to write, I also go to Google after copy! Hehe, 1 straight since want to learn json, a lot of information and write demo, finally a little understanding! Get down to business!
Let's encapsulate a class first! This class can be found online! With this class, 1 cut will become simple, haha.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Web;/// Remember to reference this namespace
using System.IO;
using System.Text;
/// <summary>
/// Summary description for JsonHelper
/// </summary>
public class JsonHelper
{
public JsonHelper()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// Serialize an object JSON String
/// </summary>
/// <typeparam name="T"> Object type </typeparam>
/// <param name="obj"> Object entity </param>
/// <returns>JSON String </returns>
public static string GetJson<T>(T obj)
{
// Remember Add Reference System.ServiceModel.Web
/**
* If you do not add the above reference ,System.Runtime.Serialization.Json; Json You can't get out.
* */
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
json.WriteObject(ms, obj);
string szJson = Encoding.UTF8.GetString(ms.ToArray());
return szJson;
}
}
/// <summary>
/// Put JSON String is restored to an object
/// </summary>
/// <typeparam name="T"> Object type </typeparam>
/// <param name="szJson">JSON String </param>
/// <returns> Object entity </returns>
public static T ParseFormJson<T>(string szJson)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
return (T)dcj.ReadObject(ms);
}
}
}
Test entity classes:
public class TestData
{
public TestData()
{
}
public int Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
}
Test page:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string jsonStr = string.Empty;
List<TestData> tds = new List<TestData>();
// Test data
for (int i = 1; i < 4; i++)
{
tds.Add(new TestData() { Id = i, Name = "jinho" + i, Sex = "male" });
} // Put 1 A list Convert to json String
jsonStr = JsonHelper.GetJson<List<TestData>>(tds);
Response.Write(jsonStr);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "json", "getJson(" + jsonStr + ");", true);
}
</script>
<script type="text/javascript">
function getJson(jsonStr) { // Use eval Function
var json = eval(jsonStr); // Because it says list Set
for (var i = 0; i < json.length; i++) {
alert(json[i].Id + "Name:" + json[i].Name);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
About json string conversion into entity, please test it yourself! As long as there is the above JsonHelper class, 1 cut is easy to handle!