Home > Web Front-end > JS Tutorial > Detailed explanation of js/jquery parsing json method

Detailed explanation of js/jquery parsing json method

php中世界最好的语言
Release: 2018-04-24 11:37:54
Original
1834 people have browsed it

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'];
Copy after login

See API for specific operations.

ps: Must be separated by square brackets.

2. Associative array

1. Syntax:

var myhash= {”key1″:”val1″, “key2″:”val2″ };//obj
Copy after login

2.var

myhash= {key1:”val1″, key2:”val2″ };//obj-也可以
Copy after login

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”;
Copy after login

2. Delete the existing key value of the Hash associative array

// 删除一个键 newkey ,同时,该键值对应的 newval 也就消失了
delete myhash[”newkey”];
Copy after login

3 .Traverse the Hash associative array

// 遍历整个hash 数组 
for (key in myhash) {
val = myhash[key];
}
Copy after login

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 array

2. 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'}];

2. To the beginning In the end, the data provided by the server to the client is only a string, so in order to enable it to perform necessary operations on it in js, it can be converted into a js executable object through eval().

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();
 }
Copy after login
 @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();
 }
Copy after login
I believe I read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template