This time I will bring you a detailed explanation of the js/jquery parsing json method. What are the precautions for js/jquery parsing json? The following is a practical case, let's take a look.
Before parsing, we must clarify several concepts: What are the differences and contact points between arrays, associative arrays and json?
1. Concept introduction1. Array
Syntax:
ECMAScript v3 specifies the syntax of array literals, JavaScript 1.2 and JScript 3.0 implement it. You can create and initialize an array by placing a comma-separated list of expressions in square brackets. The values of these expressions will become array elements. For example:
var a = [1, true, 'abc'];
See API for specific operations.
ps: Must be separated by square brackets.
2. Associative array
1. Syntax:
var myhash= {”key1″:”val1″, “key2″:”val2″ };//obj
2.var
myhash= {key1:”val1″, key2:”val2″ };//obj-也可以
ps : Almost the same as the json format, but the json format requirements are more strict (the key-value pairs inside must use double quotes), but json can only be used as a format standard. If you want to operate on it, it must be converted into an associative array object (obj) .
2. Simple operation1. Add a key value to the Hash associative array
// 添加一个新键 newkey ,键值为 newval myhash[”newkey”] = “newval”;
2. Delete the existing key value of the Hash associative array
// 删除一个键 newkey ,同时,该键值对应的 newval 也就消失了 delete myhash[”newkey”];
3 .Traverse the Hash associative array
// 遍历整个hash 数组 for (key in myhash) { val = myhash[key]; }
4.Get the value
Method 1.myhash.key1
Method 2.myhash.key2
##3.jsonFormat requirements:
{”key1″:”val1″, “key2″:”val2″ };//Strictly follow this format, and the operation can be based on the operation of the associative array2. Several key points in front-end and back-end interactions1. When the data sent by the server is not one json, but multiple jsons, it should be assembled by contacting arrays and associative arraysStringFor example: var objs = [{ id: 1, name: 'n_1' }, { id: 2, name: 'n_2'}];
Therefore, the $.parseJSON() provided in jQuey has limitations. If it is the situation mentioned in 1 above, you must use eval() for conversion, and then pass $.each(objs,function(i ,o){...}) to operate
3. Specific example codePage code:
@Override protected void service(HttpServletRequest req, HttpServletResponse reps) throws Servlet Exception , 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(); }
@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(); }
jQuery animation effect image carousel implementation (with code)
jquery drag effect implementation method
The above is the detailed content of Detailed explanation of js/jquery parsing json method. For more information, please follow other related articles on the PHP Chinese website!