繁体   English   中英

PHP json 新行和标签

[英]PHP json new lines and tabs

我在显示和格式化 JSON 代码时遇到了一些问题。 我正在使用 Laravel goutte 抓取 Aliexpress 产品页面,并且我已经提取了 aliexpress 在其源代码中提供的 JSON(您可以在源代码的末尾查看它以 Z05B8C74CBD96FBF2DE4C1A352702FFF4.runParam 开头) 因此,在我成功提取该数据后,我在格式化 JSON 时遇到了问题。 如您所见,Aliexpress 已经在 JSON 中返回了该数据,因此我不需要编写 json_encode 或解码。 I'm returning my response in postman and I don't think that postman is making that JSON response wrong, when I open JSON formatter it's always giving me errors for multiple lines "Invalid character".

这是我的完整代码,我只是调用这个 class 并在 controller 中返回它:

use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;

class Scrapers
{
    // This will be sent from another class, for now we are calling it from here
    private $client;
    public $url;
    public $array = [];

    public function __construct($url){
        $this->url = $url;
        $this->client = new Client(HttpClient::create(['timeout' => 60]));

        $this->client->request('GET', $url)->filter('script')->each(
            function ($node) {
                array_push($this->array, $node->html());
        });
    }

    public function getBetween($content,$start,$end){
        $r = explode($start, $content);
        if (isset($r[1])){
            $r = explode($end, $r[1]);
            return $r[0];
        }
        return '';
    }

    /**
     * Return product title
     */
    public function getTitle(){
        $content = $this->array[16];
        $start = "data:";
        $end = "csrfToken:";
        $output = $this->getBetween($content,$start,$end);

        $remove_new_lines = str_replace(array("\n", "\r"), '', $output);
        return $remove_new_lines;
    }

目前我没有从文件末尾提取“,”,但如果我删除它,格式化程序必须返回有效的 json 响应(但它没有)。 这就是我的 postman 中返回的数据的方式: https://justpaste.it/3qrtm

我尝试了多个删除选项卡、新空间和我在互联网上找到的所有内容的功能,但没有成功。 任何想法如何解决这个问题?

尝试$node->text()而不是$node->html()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM