Before parsing, we have to figure out several concepts: arrays, associative arrays, and what are the differences and points of contact between JSON?
I. Introduction to the Concept
1. Array
Grammar:
ECMAScript v3 Sets the syntax for the direct amount of the array, and JavaScript 1.2 and JScript 3.0 implement it. You can put-a comma-delimited list of expressions in square brackets, creating and initializing--arrays. The values of these expressions will become array elements. For example:
var a = [1, True, ' abc '];
See the API for specific actions.
PS: Must be separated by square brackets.
2. Associative array
1. Grammar:
var myhash= {"Key1″:" Val1″, "Key2″:" Val2″};//obj
2.var
myhash= {key1: "Val1″, Key2:" val2″};//obj-can also
PS: Almost the same as the JSON format, but the JSON format requirements are more stringent (inside the key value pairs must use double quotes), but JSON can only be used as a format standard, if you want to operate it must be converted to associative array objects (obj).
2. Simple operation
1. Add key value to hash associative array
Add a new key Newkey, the key value is newval
myhash["Newkey"] = "newval";
2. Delete hash associative array already has a key value
Deletes a key newkey, and the corresponding newval of the key value disappears.
Delete myhash["Newkey"];
3. Traversing hash Associative array
Traversing the entire hash array
For (key in Myhash) {
val = Myhash[key];
}
4. Get the value
Way 1.myhash.key1
Way 2.myhash.key2
3.json
Format requirements:
{"Key1″:" Val1″, "Key2″:" val2″};//strictly in this format, operations can be done according to the associative array
Two. Several key points in the front and back table interaction
1. When the server sends data that is not a JSON, but more than one JSON, the array and associative array should be contacted to assemble the string
For example: var objs = [{id:1, Name: ' N_1 '}, {id:2, Name: ' N_2 '}];
2. To the end of the server to the client data is only a string, so in order to enable it in JS to do the necessary operations, you can through eval () to convert to JS executable object.
As a result, the $.parsejson () provided in the Jquey is limited, and if this is the case mentioned in the above 1 it must be converted using eval () and then through $.each (Objs,function (i,o) {...}) To operate
Three. Specific example code
Page code:
Copy Code code as follows:
<body>
<input type= "button" value= "Send Ajax JSON" onclick= "Sendajaxbyjson ();" />
<input type= "button" value= "send Ajax Array" onclick= "Sendajaxbyarray ();" />
</body>
<script type= "Text/javascript" >
function Sendajaxbyjson () {
$.post ("json", {},function (data) {
var obj=data;
Alert (typeof obj);//string
var a=eval (obj), no comment, no error.
var strtoobj=$.parsejson (obj);
alert (strtoobj.name);
Alert (typeof Strtoobj)//obj
var obja={' name ': ' Techbirds ', ' age ': ' n ', ' sex ': ' Male '};
Alert (typeof Obja);//obj
Alert (obja[' name ']+ ":" +obja.age);
Delete obja[' name '];
});
}
function Sendajaxbyarray () {
$.post ("array", {},function (data) {
var str=data;
Alert (typeof str);//string
Alert (typeof eval (str));//object
var obja=[1,2,3,4,5];
Alert (typeof Obja);//object
});
}
</script>
Background code:
Copy Code code as follows:
@Override
protected void Service (HttpServletRequest req, HttpServletResponse Reps)
Throws Servletexception, IOException {
Map<string, object> jsonmap=new hashmap<string, object> ();
Jsonmap.put ("name", "Techbirds");
Jsonmap.put ("Age", 23);
Jsonmap.put ("Sex", "male");
Reps.getwriter (). Print (Jsonobject.fromobject (jsonmap). toString ());
Reps.getwriter (). Flush ();
Reps.getwriter (). Close ();
}
Copy Code code as follows:
@Override
protected void Service (HttpServletRequest req, HttpServletResponse Reps)
Throws Servletexception, IOException {
String array= "[1,2,3,4,5,6]";
Reps.getwriter (). print (array);
Reps.getwriter (). Flush ();
Reps.getwriter (). Close ();
}