search
HomeBackend DevelopmentPHP TutorialThe php json_encode() function returns the json data instance code, json_encodejson_PHP tutorial

php json_encode() function returns json data instance code, json_encodejson

json_encode() function usage.

echo json_encode(array('a'=>'bbbb','c'=>'ddddd');

This will generate a standard json format data

The code is as follows

<&#63;php

//需要执行的SQL语句
//单条
$sql="select id,name from tbl_user where id=1";
//多条数据
//$sql="select id,name from tbl_user";

//调用conn.php文件进行数据库操作
require('Conn.php');

//提示操作成功信息,注意:$result存在于conn.php文件中,被调用出来
if($result)
{

// $array=mysql_fetch_array($result,MYSQL_ASSOC);


/*数据集

$users=array();
$i=0;
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){

echo $row['id'].'-----------'.$row['name'].'</br>';
$users[$i]=$row;
$i++;

}
echo json_encode(array('dataList'=>$users));

*/

/*单条数据*/

$row=mysql_fetch_row($result,MYSQL_ASSOC);

echo json_encode(array('jsonObj'=>$row));
}

mysql_free_result($result);
//释放结果
mysql_close();
//关闭连接

&#63;>

The above is the json data generated by the database

Single piece of data: {"jsonObj":{"id":"1","name":"lmw"}}

Multiple pieces of data: {"dataList":[{"id":"1","name":"lmw"},{"id":"2","name":"xxj"},{ "id":"3","name":"xxxj"}]}

In many cases now, we need the program to return a result in Json format, such as:

The code is as follows

Copy code The code is as follows:

{
"UserKeyGetResponse":
{"RequestName":"e99e6d63e8c712d7699f52978a","api_key_value":"41954dd9b1cb6a95802eab6810"},
"error_response":
{"code":"NO_ERROR(www.jb51.net)","msg":"Get system parameters successfully"}
}

The result can be written in the form of an array like this:
Copy code The code is as follows:

$respon = array('UserKeyGetResponse' => array('RequestName' => $api_request_name, 'api_key_value' => $api_key_value),
'error_response' => array('code' => 'NO_ERROR', 'msg' => 'Get system parameters successfully'));

Code

Copy code The code is as follows:

function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
g:
$error_respon = array('code' => 'ERROR_MSG_MISS', 'msg' => 'Message does not exist');
echo JSON($array);

The result is:

{"code":"ERROR_MSG_MISS","msg":"Message does not exist"}
The client can parse the result. Of course, the error code must be replaced by a number.

This is much better. What we display is Chinese directly. Of course, there is no problem in displaying the hexadecimal encoding.

Teaching PHP json code examples

Use the json_decode function to decrypt the data. . The function for PHP to apply JSON is: json_encode($PHPcode);
The function for PHP to parse JSON is: json_decode($JSONcode); so there are many forms of JSON, and different forms have different forms after being interpreted by PHP. . //Form 1: It is completely in the form of an object. This form of data is also called a related array in Javascript. Different from ordinary arrays, it can be accessed through a string as an index (use "[]" or "." to represent the level) $json='{"item1":{"item11":{"n":"chenling", "m":"llll"},"sex":"male","age":"25 "},"item2": {"item21":"ling","sex":"女","age":"24"}}'; $J=json_decode($json); print_r($J); Take a look at this article about php operating json data from the PHP Beginner's Guide: www.phpnewer.com/index.php/Cjwt/detail/id/147

php code for outputting JSON data

Dear, you didn’t execute it in the environment at all. You need to compile it with php+apache

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/891103.htmlTechArticlephp json_encode() function returns json data instance code, json_encodejson json_encode() function usage. echo json_encode(array('a'='bbbb','c'='ddddd'); This will generate a standard...
Statement
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
VUE3怎么使用JSON编辑器VUE3怎么使用JSON编辑器May 12, 2023 pm 05:34 PM

1、先看看效果图,可以自行选择展示效果2、这是我在vue3项目中使用的JSON编辑器,首先引入第三方插件npminstalljson-editor-vue3yarnaddjson-editor-vue33、引入到项目中//导入模块importJsonEditorVuefrom&#39;json-editor-vue3&#39;//注册组件components:{JsonEditorVue},4、一般后端返回的是会将JSON转为String形式我们传给后端也是通过这种形式,就可以通

SpringBoot之Json的序列化和反序列化问题怎么解决SpringBoot之Json的序列化和反序列化问题怎么解决May 12, 2023 pm 04:07 PM

控制json序列化/反序列化1.@JsonIgnoreProperties的用法@JsonIgnoreProperties(value={"prop1","prop2"})用来修饰Pojo类,在序列化和反序列化的时候忽略指定的属性,可以忽略一个或多个属性.@JsonIgnoreProperties(ignoreUnknown=true)用来修饰Pojo类,在反序列化的时候忽略那些无法被设置的属性,包括无法在构造子设置和没有对应的setter方法.2.@Js

Java怎么调用接口获取json数据解析后保存到数据库Java怎么调用接口获取json数据解析后保存到数据库May 14, 2023 am 10:58 AM

Java调用接口获取json数据保存到数据库1.在yml文件中配置自己定义的接口URL//自己定义的JSON接口URLblacklist_data_url:接口URL2.在Controller中添加请求方法和路径/***@Title:查询*@Description:查询车辆的记录*@Author:半度纳*@Date:2022/9/2717:33*/@GetMapping("/Blacklist")publicvoidselectBlacklist(){booleana=imB

深入解析JWT(JSON Web Token)的原理及用法深入解析JWT(JSON Web Token)的原理及用法Jan 10, 2023 am 10:55 AM

本篇文章给大家带来了关于JWT的相关知识,其中主要介绍了什么是JWT?JWT的原理以及用法是什么?感兴趣的朋友,下面一起来看一下吧,希望对大家有帮助。

php输出json无法解析的原因和解决方法【总结】php输出json无法解析的原因和解决方法【总结】Mar 23, 2023 pm 04:35 PM

PHP作为一种常见的编程语言,在web开发中使用广泛,其与前端交互的方式也多种多样。其中,输出Json数据是一种常见的交互方式,但有时候会碰到Json无法解析的问题。为什么会出现无法解析的情况呢?下面列举了几个可能的原因。

java怎么校验json的格式是否符合要求java怎么校验json的格式是否符合要求May 15, 2023 pm 04:01 PM

JSONSchemaJSONSchema是用于验证JSON数据结构的强大工具,Schema可以理解为模式或者规则。JsonSchema定义了一套词汇和规则,这套词汇和规则用来定义Json元数据,且元数据也是通过Json数据形式表达的。Json元数据定义了Json数据需要满足的规范,规范包括成员、结构、类型、约束等。JSONSchema就是json的格式描述、定义、模板,有了他就可以生成任何符合要求的json数据json-schema-validator在java中,对json数据格式的校验,使用

php如何将xml转为json格式?3种方法分享php如何将xml转为json格式?3种方法分享Mar 22, 2023 am 10:38 AM

当我们处理数据时经常会遇到将XML格式转换为JSON格式的需求。PHP有许多内置函数可以帮助我们执行这个操作。在本文中,我们将讨论将XML格式转换为JSON格式的不同方法。

SpringBoot怎么返回Json数据格式SpringBoot怎么返回Json数据格式May 19, 2023 pm 11:49 PM

一、@RestController注解在SpringBoot中的Controller中使用@RestController注解即可返回JSON格式的数据。@RestController注解包含了@Controller和@ResponseBody注解。@ResponseBody注解是将返回的数据结构转换为JSON格式。@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Controller@Respons

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)