$json_data=file_get_contents('http://www.btc38.com/trade/getTradeList.php?coinname=XRP'); $data=json_decode($json_data,true); var_dump($data);//为什么仍然输出字符串????
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);//为什么仍然输出字符串????
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.
<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>
确实挺奇怪的,运行如上代码,使用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>
我这里没有问题啊:
<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>
保存到json_file
然后
var_dump(json_decode(file_get_contents('json_file'), true));
结果是
<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>
可能是服务器不太稳定,传输的数据错误了。
求截图...

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

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.

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.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


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

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)