Table of Contents
回复内容:
Home Backend Development PHP Tutorial json返回数据格式的奇怪问题?

json返回数据格式的奇怪问题?

Jun 06, 2016 pm 08:47 PM
apache json php

在本地的环境中 使用laravel3 返回的json数据都是正常的
例如{"id":1}

但把代码传到开发机后 返回的数据变成了
{"id":"1"}
就是所有的值全加上了双引号, 全变成了字符串类型, 非常奇怪,不知道是服务器apache的设置问题还是什么?

header 头部 已经是 Content-Type application/json; charset=UTF-8

本地环境 PHP5.4 mysql 5.6
开发机环境 PHP5.3 mysql 5.0

目前暂定问题 是 PHP5.3的链接mysql驱动问题 PHP从5.3开始才可以原生返回mysql字段类型,
http://stackoverflow.com/questions/1197005/how-to-get-numeric-types-from-mysql-using-pdo#answer-1197041

现在的问题是要安装 mysqlnd 在Centos6.4, 的PHP5.3上, 但默认包里面没有,不知道如何安装?
http://stackoverflow.com/questions/13159518/how-to-enable-mysqlnd-for-php

回复内容:

在本地的环境中 使用laravel3 返回的json数据都是正常的
例如{"id":1}

但把代码传到开发机后 返回的数据变成了
{"id":"1"}
就是所有的值全加上了双引号, 全变成了字符串类型, 非常奇怪,不知道是服务器apache的设置问题还是什么?

header 头部 已经是 Content-Type application/json; charset=UTF-8

本地环境 PHP5.4 mysql 5.6
开发机环境 PHP5.3 mysql 5.0

目前暂定问题 是 PHP5.3的链接mysql驱动问题 PHP从5.3开始才可以原生返回mysql字段类型,
http://stackoverflow.com/questions/1197005/how-to-get-numeric-types-from-mysql-using-pdo#answer-1197041

现在的问题是要安装 mysqlnd 在Centos6.4, 的PHP5.3上, 但默认包里面没有,不知道如何安装?
http://stackoverflow.com/questions/13159518/how-to-enable-mysqlnd-for-php

应该跟 apache 无关,是程序代码的问题吧,是不是在哪里把数字转换成字符串了?

可以在数据返回前再做下转换成整型的操作么

自问自答,目前暂定为是Mysql驱动的事情 , 不知道有人遇到过吗?

http://stackoverflow.com/questions/1197005/how-to-get-numeric-types-from-mysql-using-pdo#answer-1197041

请贴出你的 PHP 版本,以及使用到 json_encode 时,所用参数的来源,因为很有可能你的数据来源是数据库的时候,数据库里定义的类型是字符串,于是在 json_encode 的时候,就变成了字符串。

我用的 PHP 版本是 5.4.23,直接用硬编码做了 json_encode 的测试:

<?php
require_once 'zend_autoload.php';

$json = [
    'name'   => 'caiknife',
    'age'    => 18,
    'height' => '175'
];

Zend_Debug::dump(json_encode($json));

Zend_Debug::dump(Zend_Json::encode($json));
Copy after login

输出结果:

<code>string '{"name":"caiknife","age":18,"height":"175"}' (length=43)
string '{"name":"caiknife","age":18,"height":"175"}' (length=43)
</code>
Copy after login

你是在返回的时候将JSON对象toString了么?

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://&lt;server IP or domain name&gt;/server-status), or view the Apache configuration file (ServerVersion: Apache/&lt;version number&gt;).

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

See all articles