Detailed explanation of js/jquery parsing json method
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

JSON (JavaScriptObjectNotation) is a lightweight data exchange format commonly used for data exchange between web applications. When processing JSON data, we often encounter Unicode-encoded Chinese characters (such as "u4e2du6587") and need to convert them into readable Chinese characters. In PHP, we can achieve this conversion through some simple methods. Next, we will detail how to convert JSONUnico

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
