Table of Contents
回复内容:
后记
Home Backend Development PHP Tutorial json_decode一个json字符为什么还是字符

json_decode一个json字符为什么还是字符

Jun 06, 2016 pm 08:48 PM
json php

$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP');
$data=json_decode($json_data,true);
var_dump($data);//为什么仍然输出字符串????
Copy after login
Copy after login

ps:在jslint.com 测试$json_data为valid

回复内容:

$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP');
$data=json_decode($json_data,true);
var_dump($data);//为什么仍然输出字符串????
Copy after login
Copy after login

ps:在jslint.com 测试$json_data为valid

BOM!

我直接 echo 从网页获取到的内容,然后传递给 json_pp:

>>> php a.php | json_pp
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "\x{ef}\x{bb}\x{bf}{"...") at /usr/bin/core_perl/json_pp line 44.
Copy after login

<code><?php
$header = array(
    "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
);
$curl = curl_init('http://www.btc38.com/trade/getTradeList.php?coinname=XRP');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$res = curl_exec($curl);
curl_close($curl);

$arr = json_decode($res);

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo '没有错误发生';
    break;
    case JSON_ERROR_DEPTH:
        echo '到达了最大堆栈深度';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo '无效或异常的 JSON';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo '控制字符错误,可能是编码不对';
    break;
    case JSON_ERROR_SYNTAX:
        echo '语法错误';
    break;
    case JSON_ERROR_UTF8:
        echo '异常的 UTF-8 字符,也许是因为不正确的编码。';
    break;
    default:
        echo '未知错误';
    break;
}
?>
</code>
Copy after login

确实挺奇怪的,运行如上代码,使用json_last_error()最终提示的是语法错误。根据错误关键词搜索到SO中的这个问题:json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK 情况比较类似。按照答案之一说的怀疑了一下编码问题,不过看了下是UTF-8,没有问题。

单纯的复制页面中的字符串的话是没有问题的(复制到页面和PHP中都没有问题)。这个不算是答案,算是帮助题主补充说明吧,我也还在努力的寻找答案中。

后记

感谢 @依云 的答案,让我明白了原来是可恶的BOM的问题,关于BOM的问题 @依云 的维基百科链接已经很详细了,我就不多说了,这里就说一下怎么去除吧。其实BOM就是在字符串的最开头增加了三个字符,我们把它去除掉就好了。在json_decode之前用substr去除就好了,例如:

<code>$res = substr($res, 3);
$arr = json_decode($res, true);
</code>
Copy after login

我这里没有问题啊:

<code>{"buyOrder":[{"price":"0.222000","amount":"63077.877631"},{"price":"0.221000","amount":"82921.415688"},{"price":"0.220000","amount":"53624.458032"},{"price":"0.219000","amount":"120232.956415"},{"price":"0.218000","amount":"58609.097966"},{"price":"0.217000","amount":"48330.724883"},{"price":"0.216000","amount":"15955.233203"},{"price":"0.215000","amount":"146085.153646"},{"price":"0.214000","amount":"17075.000000"},{"price":"0.213000","amount":"18729.000000"}], "sellOrder":[{"price":"0.223000","amount":"10724.828662"},{"price":"0.224000","amount":"133336.648858"},{"price":"0.225000","amount":"140519.661232"},{"price":"0.226000","amount":"10095.282427"},{"price":"0.227000","amount":"26586.879929"},{"price":"0.228000","amount":"27247.504336"},{"price":"0.229000","amount":"35233.324750"},{"price":"0.230000","amount":"84477.240158"},{"price":"0.231000","amount":"17260.931218"},{"price":"0.232000","amount":"72333.275935"}], "trade":[{"price":"0.223000","volume":"52.322237","time":"2013-12-12 18:41:21","type":"1"},{"price":"0.222000","volume":"1.000000","time":"2013-12-12 18:41:02","type":"2"},{"price":"0.223000","volume":"849.956784","time":"2013-12-12 18:37:45","type":"1"},{"price":"0.223000","volume":"8181.433350","time":"2013-12-12 18:37:45","type":"1"},{"price":"0.223000","volume":"1696.845442","time":"2013-12-12 18:34:42","type":"1"},{"price":"0.223000","volume":"63.826185","time":"2013-12-12 18:34:42","type":"1"},{"price":"0.222000","volume":"322.039509","time":"2013-12-12 18:33:57","type":"2"},{"price":"0.222000","volume":"676.960491","time":"2013-12-12 18:33:57","type":"2"},{"price":"0.222000","volume":"3628.741136","time":"2013-12-12 18:33:40","type":"2"},{"price":"0.223000","volume":"0.672054","time":"2013-12-12 18:33:31","type":"1"},{"price":"0.222000","volume":"1.000000","time":"2013-12-12 18:33:11","type":"2"},{"price":"0.223000","volume":"442.969488","time":"2013-12-12 18:32:41","type":"1"},{"price":"0.222000","volume":"2.455251","time":"2013-12-12 18:31:49","type":"2"},{"price":"0.223000","volume":"20.715520","time":"2013-12-12 18:28:48","type":"1"},{"price":"0.223000","volume":"465.314511","time":"2013-12-12 18:26:54","type":"1"},{"price":"0.223000","volume":"6.502242","time":"2013-12-12 18:26:50","type":"1"},{"price":"0.224000","volume":"35.229334","time":"2013-12-12 18:22:31","type":"1"},{"price":"0.224000","volume":"100.000000","time":"2013-12-12 18:22:13","type":"1"},{"price":"0.223000","volume":"1609.883424","time":"2013-12-12 18:22:07","type":"2"},{"price":"0.223000","volume":"99.900000","time":"2013-12-12 18:21:35","type":"2"}]}
</code>
Copy after login

保存到json_file

然后

var_dump(json_decode(file_get_contents('json_file'), true));
Copy after login

结果是

<code>array(3) {
  ["buyOrder"]=>
  array(10) {
    [0]=>
    array(2) {
      ["price"]=>
      string(8) "0.222000"
      ["amount"]=>
      string(12) "63077.877631"
    }
    [1]=>
    array(2) {
      ["price"]=>
      string(8) "0.221000"
      ["amount"]=>
      string(12) "82921.415688"
    }
以下省略...
</code>
Copy after login

可能是服务器不太稳定,传输的数据错误了。

求截图...

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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles