Learn to read 145 comments 01:55:59 0 font size: LargeMediumSmall subscription
JSON (JavaScript Object Notation) is a simple data format, which is lighter than XML. JSON is a javascript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
JSON rules are simple: the object is an unordered "'name/value' pair" set. An object starts with "{" (left brace) and ends with "}" (right brace. Each "name" is followed by a ":" (colon); "Use commas (,) to separate 'name/value' pairs. Detailed reference http://www.json.org/json-zh.html
Example: A: [{"name": "zhangsan"}]
B: [{"name": "zhangsan", "sex": "Man"}]
C: [{"name": "zhangsan", "sex": "Man" },{ "name": "Lili", "sex": "woman"}]
Each expansion number in it is one item. for c json, we can use C [0]. name to retrieve the zhangsan field, C [1]. sex gets the sex "woman" corresponding to Lili and the second item"
In this way, we can easily upload the values of each field in the database to the page. Here I will use the MVC2 architecture. The databases used are as follows:
Here I will only demonstrate the usage in a simple way. As for the page effect, there will be no exquisite design. The page is very simple. Just click the button to display the powername and powercnname of the second item.
The view layer is as follows:
<Head runat = "server">
<Title> test </title>
<SCRIPT src = ".../scripts/jquery-1.4.1.js" type = "text/JavaScript"> </SCRIPT>
</Head>
<Body>
<Div>
Permission: <input type = "text" id = "name"/> <br/>
Chinese name: <input type = "text" id = "cname"/> <br/>
<Input type = "button" id = "show" value = "Show second"/>
</Div>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ("# Show"). Click (function getdate (){
$. Ajax ({
URL: "getpower ",
Type: "Post ",
Datatype: "JSON ",
Success: getdataresult
});
});
Function getdataresult (data ){
$ ("# Name"). Val (data [1]. Name );
$ ("# Cname"). Val (data [1]. chinesename );
}
});
</SCRIPT>
</Body>
The model layer creates a class named power, specifically responsible for reading data.
Public class power
{
Crmdatacontext DB = new crmdatacontext ();
Public list <crm_power> getallpowers ()
{
Return dB. crm_power.tolist ();
}
}
The Controller layer mainly writes an action for Ajax to call JSON.
Public actionresult test ()
{
Return view ();
}
Public actionresult getpower ()
{
Power P = new power ();
VaR powers = P. getallpowers ();
Arraylist list = new arraylist ();
Foreach (VAR s in powers)
{
Hashtable HA = new hashtable ();
Ha. Add ("name", S. powername );
Ha. Add ("chinesename", S. powercnname );
List. Add (HA );
}
Return this. JSON (list );
}
The page effect is as follows. If you use firebug debugging, you can also find the corresponding page as follows: