Convert a JSON string to a C # Class Object

Source: Internet
Author: User
Problem description: The editorgridpanel of extjs can be modified and saved at one time in batches.

 

1. Save. js ---------------------------------------
VaR JSON = ""; // input the background JSON string
VaR flag = false; // whether to add a comma flag
VaR grid = new Ext. Grid. editorgridpanel ({
Clickstoedit: 1,
Listeners :{
Afteredit: function (e ){
If (FLAG ){
JSON + = ',';
}
Flag = true;
JSON + = '{"mid":' + '"' + E. Record. Data. m_id
+ '"," Ecode ":' + '"' + E. Value + '"}';
}
}

});
});
// JSON = "[{\" mid \ ": \" 3 \ ", \" ecode \ ": \" 2900.66 \ "},{ \" mid \": \ "4 \", \ "ecode \" :\" 2348.52 \ "}]";
Function savelotinfotodb () // click 'save' to transfer data to the savadata. ASPX page.
{
Ext. Ajax. Request ({
URL: '/savedata. aspx ',
Params :{
JSON: JSON
}, // Parameters uploaded to the background
Success: function (R, O ){
Ext. msg. Alert ('hprompt ',' modified successfully! ');
},
Failure: function (){
Ext. msg. Alert ("prompt", "failed to save! ");
}
});
}
2. savedata. aspx. CS ---------------------------------------
Protected void page_load (Object sender, eventargs E)
{
Save ();
}
/// <Summary> author: It Mongolian
/// Deserialize the JSON string into a net object
/// </Summary>
Public list <freedata> isefect (string JSON)
{
Memorystream stream2 = new memorystream ();
Datacontractjsonserializer ser2 = new datacontractjsonserializer (typeof (list <freedata> ));
Streamwriter wR = new streamwriter (stream2 );
Wr. Write (JSON );
Wr. Flush ();
Stream2.position = 0;
Object OBJ = ser2.readobject (stream2 );
List <freedata> List = (list <freedata>) OBJ;
Return list;
}

Public void save ()
{
String JSON = "[" + request ["JSON"] + "]";
String SQL = NULL;
List <freedata> ls = isefect (JSON );
For (INT I = 0; I <ls. Count; I ++)
{
Int id = ls [I]. Mid;
Float code = ls [I]. ecode); // SQL + = "Update userinfo set salary =" + code + "where id =" + ID + ";";
}
// Return sqlecute (SQL); // execute the database update statement to save (multiple) data records.
}
---------------------------------------------------------------
Note: Pay attention to the freedata class here. The property name in the freedata class must be prefixed with the key name of the JSON data with "_", so that the LS [I] in the SAVE () function will be added. mid, ls [I]. the ecode gets the correct value (this is not very positive, but I cannot get the value when I first wrote the attribute name to another one ).
3. freedata. CS ---------------------------------------
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;

/// <Summary> author: It Mongolian
/// Freedata Summary
/// </Summary>
Public class freedata
{
Private int _ mid;
Private float _ ecode;

Public freedata ()
{}
Public float ecode
{
Get {return _ ecode ;}
Set {This. _ ecode = value ;}
}
Public int mid {
Get {return _ mid ;}
Set {_ mid = value ;}
}
}
========================================================== ========================================================== ==========
As a result, the strings passed by extjs (containing multiple pieces of data) are converted into C # objects and the values are obtained.
Comments from readers!

 

Author: It Mongols

Source: http://hi.baidu.com/qing_bd_liang/item/537cf93751e063ca2e8ec28b

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.