一、使用 json_encode 輸出
<?php header('content-type:application/json;charset=utf8'); $arr = array( 'status' => true, 'errMsg' => '', 'member' =>array( array( 'name' => '李', 'gender' => '男' ), array( 'name' => '趙', 'gender' => '女' ) ) ); echo json_encode($arr); ?>
(推薦學習:PHP視頻教程)
輸出:
{"status":true,"errMsg":"","member":[{"name":"\u674e","gender":"\u7537"},{"name":"\u8d75","gender":"\u5973"}]}
二、使用 jsonFormat 輸出
<?php /** Json數(shù)據(jù)格式化 * @param Mixed $data 數(shù)據(jù) * @param String $indent 縮進字符,默認4個空格 * @return JSON */ function jsonFormat($data, $indent=null){ // 對數(shù)組中每個元素遞歸進行urlencode操作,保護中文字符 array_walk_recursive($data, 'jsonFormatProtect'); // json encode $data = json_encode($data); // 將urlencode的內(nèi)容進行urldecode $data = urldecode($data); // 縮進處理 $ret = ''; $pos = 0; $length = strlen($data); $indent = isset($indent)? $indent : ' '; $newline = "\n"; $prevchar = ''; $outofquotes = true; for($i=0; $i<=$length; $i++){ $char = substr($data, $i, 1); if($char=='"' && $prevchar!='\\'){ $outofquotes = !$outofquotes; }elseif(($char=='}' || $char==']') && $outofquotes){ $ret .= $newline; $pos --; for($j=0; $j<$pos; $j++){ $ret .= $indent; } } $ret .= $char; if(($char==',' || $char=='{' || $char=='[') && $outofquotes){ $ret .= $newline; if($char=='{' || $char=='['){ $pos ++; } for($j=0; $j<$pos; $j++){ $ret .= $indent; } } $prevchar = $char; } return $ret; } /** 將數(shù)組元素進行urlencode * @param String $val */ function jsonFormatProtect(&$val){ if($val!==true && $val!==false && $val!==null){ $val = urlencode($val); } } header('content-type:application/json;charset=utf8'); $arr = array( 'status' => true, 'errMsg' => '', 'member' =>array( array( 'name' => '李', 'gender' => '男' ), array( 'name' => '趙', 'gender' => '女' ) ) ); echo jsonFormat($arr); ?>
輸出:
{ "status":true, "errMsg":"", "member":[ { "name":"李", "gender":"男" }, { "name":"趙", "gender":"女" } ] }
三、php5.4 以后,json_encode增加了JSON_UNESCAPED_UNICODE , JSON_PRETTY_PRINT 等幾個常量參數(shù)。使顯示中文與格式化更方便。
<?php header('content-type:application/json;charset=utf8'); $arr = array( 'status' => true, 'errMsg' => '', 'member' =>array( array( 'name' => '李', 'gender' => '男' ), array( 'name' => '趙', 'gender' => '女' ) ) ); echo json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
輸出:
{ "status": true, "errMsg": "", "member": [ { "name": "李", "gender": "男" }, { "name": "趙", "gender": "女" } ] }
The above is the detailed content of Methods for formatting (beautifying) php JSON data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

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.

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.

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

When we edit word documents, we always hope to make the documents more beautiful and beautiful. However, when it comes to word beautification, many people think of making the fonts and colors more personalized, and adjusting the margins and line spacing. Wait, in fact, we can make word more beautiful through more operations. For example, we can make word documents more beautiful by inserting pictures, modifying borders, etc. Next we will try to use border patterns to make word documents more beautiful, let’s learn together! First, open a new Word document, and then find the [Paragraph] tool under the [Home] tab. Next, click the [Border] option, as indicated by the red arrow in the image. 2. After we click, the system will automatically pop up a drop-down selection

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.

Parsing JSON data Parsing JSON data is a critical step in processing complex data. In Java, we can use the following methods: Use the Gson library: Gson is a widely used jsON parsing library that provides a concise and efficient api, as shown below: Gsongson=newGson();JsonObjectjsonObject=gson.fromJson(jsonString ,JsonObject.class); Using the Jackson library: Jackson is another popular JSON processing library that supports rich functionality and conversion to other formats (such as XML), as shown below: ObjectMappe

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.
