A brief discussion on json objects and array values
Value by object:
jQueryThe code is as follows
(function ($) { $.getJSON('ajax/test.json', function (data) { var items = []; $.each(data.comments, function (key, val) { items.push('<li class="' + 'tag' + val.class + '">' + '<a href="#">' + val.content + '</a>' + '</li>'); }); //第一个标签 $('<ul/>', { 'class':'', html:items.join('') }).appendTo('.tags'); //第二个标签 $('<ul/>', { 'class':'alt', html:items.join('') }).appendTo('.tags'); }); })(jQuery);
json code is as follows
{"comments":[ { "class":"1", "content":"Lorem ipsum" }, { "class":"2", "content":"Dolor sit amet" }, { "class":"3", "content":"Consectetur adipiscing elit" }, { "class":"2", "content":"Proin" }, { "class":"4", "content":"Sagittis libero" }, { "class":"1", "content":"Aliquet augue" }, { "class":"1", "content":"Quisque dui lacus" }, { "class":"5", "content":"Consequat" }, { "class":"2", "content":"Dictum non" }, { "class":"1", "content":"Venenatis et tortor" }, { "class":"3", "content":"Suspendisse mauris" }, { "class":"4", "content":"In accumsan" }, { "class":"1", "content":"Egestas neque" }, { "class":"5", "content":"Mauris eget felis" }, { "class":"1", "content":"Suspendisse" }, { "class":"2", "content":"condimentum eleifend nulla" } ]}
According to arrayValue:
jQuery code is as follows
(function ($) { $.getJSON('ajax/test_array.json', function (data) { var items = []; $.each(data.comments, function (key, val) { items.push('<li class="' + 'tag' + val[0] + '">' + '<a href="#">' + val[1] + '</a>' + '</li>'); }); //第一个标签 $('<ul/>', { 'class':'', html:items.join('') }).appendTo('.tags'); //第二个标签 $('<ul/>', { 'class':'alt', html:items.join('') }).appendTo('.tags'); }); })(jQuery);
json code is as follows
{"comments":[ ["1", "Lorem ipsum"], ["2", "Dolor sit amet"], ["3", "Consectetur adipiscing elit"], ["2", "Proin"], ["4", "Sagittis libero"], ["1", "Aliquet augue"], ["1", "Quisque dui lacus"], ["5", "Consequat"], ["2", "Dictum non"], ["1", "Venenatis et tortor"], ["3", "Suspendisse mauris"], ["4", "In accumsan"], ["1", "Egestas neque"], ["5", "Mauris eget felis"], ["1", "Suspendisse"], ["2", "condimentum eleifend nulla"] ]}
The shared HTML code is as follows
<p class="tags"></p>
It can be clearly seen that fetching by array The data volume of the value will be much smaller
The above is the detailed content of A brief discussion on json objects and array values. 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

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

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 functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

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.
