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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Apache in Action: Web Servers and Web Applications Apache in Action: Web Servers and Web Applications Apr 28, 2025 am 12:21 AM

The main functions of ApacheHTTPServer include modular design, virtual host configuration and performance optimization. 1. Modular design implements functions by loading different modules, such as SSL encryption and URL rewriting. 2. Virtual host configuration allows multiple websites to be run on one server. 3. Performance optimization improves performance by adjusting parameters such as ServerLimit and KeepAlive.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Using Apache: Building and Hosting Websites Using Apache: Building and Hosting Websites Apr 25, 2025 am 12:07 AM

Apache is an open source web server software that is widely used in website hosting. Installation steps: 1. Install using the command line on Ubuntu; 2. The configuration file is located in /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf. Through module extensions, Apache supports static and dynamic content hosting, optimizes performance and security.

See all articles