php如何判断是否为json数据格式
json format
下面我来给各位朋友总结了几篇关于php如何判断是否为json数据的文章,这里整理了一下也能让你知道如何利用php判断json了,希望例子对你带来帮助.
首先要记住json_encode返回的是字符串,而json_decode返回的是对象.
判断数据不是JSON格式,代码如下:
function is_not_json($str){ return is_null(json_decode($str)); }
Copy after login
判断数据是合法的json数据,PHP版本大于5.3,代码如下:
function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); }
Copy after login
json_last_error()函数返回数据编解码过程中发生的错误.
注意:json编解码所操作字符串必须是UTF8的.
例子,代码如下:
<?php /** * 解析json串 * @param type $json_str * @return type */ function analyJson($json_str) { $json_str = str_replace('\\', '', $json_str); $out_arr = array(); preg_match('/{.*}/', $json_str, $out_arr); if (!emptyempty($out_arr)) { $result = json_decode($out_arr[0], TRUE); } else {//开源代码phprm.com return FALSE; } return $result; }
Copy after login
如果不是json则返回false.
文章网址:
随意转载^^但请附上教程地址。
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

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
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
1 months ago
By DDD
R.E.P.O. Best Graphic Settings
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
1 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

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)
