C # and JavaScript to manipulate the implementation code of the same JSON object _javascript tips

Source: Internet
Author: User
Can I have the client and server operate the same JSON object? The way you think about it now is through a client-side hidden control.
The following is a generic list object list<trainingimplement>, which, when converted to JSON, operates on the client and server side
1, JSON object and C # generic conversion code
Copy Code code as follows:

Convert JSON data to generics
public static T convertbytedatatoobject<t> (String bytedata)
{
T obj;
using (var ms = new MemoryStream (Encoding.UTF8.GetBytes (bytedata))
{
var serializer = new DataContractJsonSerializer (typeof (T));
obj = (T) serializer. ReadObject (MS);
}
return obj;
}
Convert generics to JSON
public static string convertobjecttobytedata<t> (T obj)
{
string result;
using (var ms = new MemoryStream ())
{
var serializer = new DataContractJsonSerializer (typeof (T));
Serializer. WriteObject (MS, obj);
Ms. Position = 0;
result = Encoding.UTF8.GetString (Ms. ToArray ());
}
return result;
}

2. The JSON data source is stored in the client-side hidden control
Copy Code code as follows:

<input type= "hidden" id= "Hidedatasource" runat= "Server"/>

3. Note that hiding the control after the JSON data, because contains "/" will make requests for instructions error, so the page header please set validaterequest= "false"
Copy Code code as follows:

<%@ Page language= "C #" validaterequest= "false" autoeventwireup= "true"

4, when the page load Page_Load, initialize the JSON data source
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
#region Load a data source
if (! IsPostBack)
{
list<trainingimplement> list= New list<trainingimplement> () {
New Trainingimplement () {
Code= "AAA",
C_name = "BBB"
}
....
}//Initialization Data source
Hidedatasource.value = Convertobjecttobytedata (list);
}
Else
{
If it is a postback, the data source is read from the client
list<trainingimplement> list = convertbytedatatoobject<list<trainingimplement>> ( Hidedatasource.value);
Hidedatasource.value = Convertobjecttobytedata (list);
}
#endregion

4. Client JS operation JSON data source sample
Copy Code code as follows:

<script type= "Text/javascript" src= ". /scripts/jquery-1.4.3.js "></script>
<script type= "Text/javascript" src= ". /scripts/jquery-ui-1.8.7.custom.min.js "></script>
<script type= "Text/javascript" src= ". /scripts/jquery.json-2.2.min.js "></script>
<script type = "Text/javascript" >
var datasourcehidname = "Hidedatasource";
var datasourcedom;
var Datasourcejson;
$ (document). Ready
(function () {
Get Data source
Datasourcedom = document.getElementById (datasourcehidname);
Datasourcejson = eval ("+ Datasourcedom.value +"));
});
Example method for modifying the code value of the 1th Trainingimplement object
function Modifiedcode () {
Datasourcejson[0]._code = "Code001";
Re-writes a JSON object with the updated value to the hidden control
Datasourcedom.value = $.tojson (Datasourcejson);
}
</script>

5. Service-side C # operations JSON data source
Copy Code code as follows:

list<trainingimplement> list = convertbytedatatoobject<list<trainingimplement>> ( Hidedatasource.value);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.