How jquery reads data from json and appends it to html
jQuery is a cross-browser JavaScript library that simplifies operations between HTML and JavaScript. In practical applications, when there are Chinese characters, using the json_encode() function directly will cause the Chinese characters to be encoded in the form of "\u***". This problem has been solved since php5.4. Use the following method to solve the problem of Chinese characters being encoded. question. In this article, we mainly introduce to you the relevant information on how to use jquery to read data from json and append it to html.
JSON format
json is the most frequently used data format in Ajax for communication between the browser and the server Can't live without it.
JSON format description
It is important to note that attribute names in JSON need to be enclosed in quotation marks.
1. Download and install jquery
You can introduce the online version of js through the following method:
1 |
|
Refer to the installation documentation: http://www.jb51.net/zt/jquerydown.htm
2. Prepare a file in json format. The suffix may not be .json
For example, the following is the format of result.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
3. Obtain the data of the Json file through $.getJSON
For example, the following example: read the contents of the result.json file and store it in the result variable. The result is a json format
##
1 |
|
4. Add the html content behind the positioned element through [$('#element id').after(html content);]
##
1 2 3 |
|
##Insert html content position:
- prepend() - Insert content at the beginning of the selected elements
- after() - Insert content after the selected element
- before() - Insert content before the selected element
- Json data operation
JSON object [key] to read the content: result['title'], or use result."title"
The object value of the array can be obtained through $.each:
$.each(JSON array object, function (traverse index i, traverse object) {operate the traversed object})
Read result.json and append html code as follows
(jquery needs to be written in the <script> tag)</p><p> <strong></strong><br/></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
//使用getJSON方法读取json数据,
//注意:info.json可以是不同类型文件,只要其中的数据为json类型即可
$.getJSON('./result.json',function(result){
var html_title='';
var html_resultinfo='';
html_title += '<b>'+result["title"]+'</b>';
$('#resultitle').after(html_title);
$.each(result["resultinfo"],function(i,item){
if(item["pass"]=="true") {
html_resultinfo += '<tr><td>' + item['name'] + '</td>' +
'<td>' + item['moudle'] + '</td>' +
'<td>' + item["pass"] + '</td>' +
'<td>' + item['onecepass'] + '</td>' +
'<td id="' + item['moudle'] + '" class="collapsed" onclick="collapsedisplay(' + item['moudle'] + ')"><u style="color: blue;">展开</u></td></tr>';
html_resultinfo +='<tr id="' + item['moudle'] + 'info" class="collapsedinfo" style="display:none"><td colspan="5">' + item['log'] + '</td></tr>';
}
$('#infotitle').after(html_resultinfo);//after方法:在每个匹配的元素之后插入内容。
});
});
</script>
用例名称 | 模块名称 | 是否成功 | 一次成功 | 详情 |
---|
JQuery method summary of obtaining elements
jQuery loads an html page into the specified p
js method of reading and parsing JSON data
The above is the detailed content of How jquery reads data from json and appends it to html. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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 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.

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 data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

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:
