【问题标题】:php, json SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1php, json SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1
【发布时间】:2024-01-23 15:22:01
【问题描述】:

test.php

  <?php

  require_once __DIR__ . '/vendor/autoload.php';

  $urlPaczkomaty =  "http://api.paczkomaty.pl/?do=listmachines_xml";
  $curl = new Curl\Curl();
  $curl->get(  $urlPaczkomaty  );

  $xml = simplexml_load_string($curl->rawResponse);
  $json =  utf8_encode (  json_encode($xml)  );
  $arrList = json_decode($json, TRUE);

  $fieldsToOut = array(
      'name',
      'type',
      'postcode',
      'province',
      'street',
      'town',
      'latitude',
      'longitude',
      'paymentavailable',
      'status'
  );

  $key = "machine";

  if( empty($arrList[ $key ] ) ){
      throw new \Exception( "no key" );
  }
  $out = array();
  $i = 0;
  foreach( $arrList[ $key ] as $item  ){
      foreach(  $fieldsToOut as $field ){
          if( empty( $field  )  ){
              die( 'iii' );
          }
          if( empty($item[ $field ]) ){
              die(" no key: ". $field. "  in = ".( empty($item[ 'name' ]) ? '' : $item[ 'name' ] )  );
              $item[ $field ] = 'xxxxx';
          }
          $out[ $i ][$field] = $item[ $field ];
      }
      $i++;
  }

header('Content-Type: application/json');
echo json_encode($out);

composer.json

{
    "require": {
        "php-curl-class/php-curl-class": "^4.10"
    }
}

在 Firefox 中,当我执行(debian、apache)时: http://test.loc/test.php 我收到以下错误(不一样):

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 129650 of the JSON data

or

SyntaxError: JSON.parse: expected ':' after property name in object at line 1 column 261200 of the JSON data

在我的本地环境(即 xamp)中,Windows 10 可以正常工作。

有什么建议吗?

最好的问候 罗伯特。

【问题讨论】:

  • 首先我建议将JSON_PRETTY_PRINT 传递给json_encode。这将为您提供格式化的 JSON,因此您可以获得比“第 261200 列”更有用的内容...之后,查看正在读取的 JSON,特别是(更新的)错误消息中指示的行。

标签: php json apache firefox curl


【解决方案1】:

问题解决了

我删除了firefox中解析json的插件(插件)。

罗伯特

【讨论】: