Before parsing, we must understand several concepts: what are the differences and contact points between arrays, associated arrays, and json?
1. Introduction to concepts
1. Array
Syntax:
ECMAScript v3 specifies the syntax for the direct amount of arrays, which is implemented by JavaScript 1.2 and JScript 3.0. You can put a comma-separated expression list in square brackets to create and initialize an array. The values of these expressions become array elements. For example:
Var a = [1, true, 'abc'];
For more information, see the API.
Ps: must be separated by square brackets.
2. Join an array
1. Syntax:
Var myhash = {"key1": "val1", "key2": "val2"}; // obj
2. var
Myhash = {key1: "val1", key2: "val2"}; // obj-can also
Ps: The json format is almost the same as the json format, but the json format is more strict (the key-value pair must use double quotation marks). However, json can only be used as a standard format, if you want to operate on it, you must convert it to an associated array object (obj ).
2. Simple operation
1. Add a key value to the Hash join Array
// Add a new key newkey with the key value newval
Myhash ["newkey"] = "newval ";
2. Delete existing key values of the Hash associated array
// Delete a key newkey. At the same time, the newval corresponding to the key value disappears.
Delete myhash ["newkey"];
3. traverse the Hash associated array
// Traverse the entire hash Array
For (key in myhash ){
Val = myhash [key];
}
4. Obtain the value
Method 1. myhash. key1
Method 2. myhash. key2
3. json
Format requirements:
{"Key1": "val1", "key2": "val2"}; // strictly follow this format. operations can be performed based on the associated array.
2. Several key points in front-and-back Interaction
1. When the data sent by the server is not a json, but multiple json, you should contact the array and the associated array to assemble the string.
For example, var objs = [{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}];
2. the data from the beginning to the end server to the client is only a string, so in order to allow it to perform necessary operations on it in js, you can use eval () convert to JavaScript executable objects.
Therefore, the $. parseJSON () is limited. If this is the case mentioned in 1 above, you must use eval () for conversion and then use $. each (objs, function (I, o ){...}) operation
Iii. specific instance code
Page code:
Copy codeThe Code is 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); puzzled. An error is returned if no comment is made ..
Var strToobj = $. parseJSON (obj );
Alert (strToobj. name );
Alert (typeof strToobj) // obj
Var obja = {'name': 'birds ', 'age': '23', '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, 4, 5];
Alert (typeof obja); // object
});
}
</Script>
Background code:
Copy codeThe Code is 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 codeThe Code is 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 ();
}