1

我有以下 php 数组,并希望将其打印为干净的 JSON 文件,但括号和逗号周围有额外的引号。虽然我得到的当前 JSON 是有效的 JSON,但它不是我想要的格式。请参阅下面的 PHP 数组、我的 json_encode 输出和首选 json 输出:

    Array
 (
[0] =>             {
[1] =>                 code : "EU27",
[2] =>                 debtA : 967576248244,
[3] =>                 debtB : 6971817122583,
[4] =>                 id : "eur",
[5] =>                 name : "EU",
[6] =>                 popY : 8584663069945,
[7] =>                 popZ : 7565913300548            }
[8] =>             ,
[9] =>             {
[10] =>                 code : "US",
[11] =>                 debtA : 9090198981283,
[12] =>                 debtB : 92189801202,
[13] =>                 id : "usa",
[14] =>                 name : "United States",
[15] =>                 popY : 4514703786570,
[16] =>                 popZ : 774697283542            }
    )

当前 json_encode 输出:

   [
" {",
" code : \"EU27\",",
" debtA : 1739414366187,",
" debtB : 8719158563660,",
" id : \"eur\",",
" name : \"EU\",",
" popY : 460572909944,",
" popZ : 2396933253407 }",
" ,",
" {",
" code : \"US\",",
" debtA : 7810766144794,",
" debtB : 10536751929567,",
" id : \"usa\",",
" name : \"United States\",",
" popY : 8571487476842,",
" popZ : 1716024078740 }"
    ]

首选 JSON 输出:

   [
 {
  code : "EU27",
  debtA : 1739414366187,
  debtB : 8719158563660,
  id : "eur",
  name : "EU",
  popY : 460572909944,
  popZ : 2396933253407 },
{
  code : "US",
  debtA : 7810766144794,
  debtB : 10536751929567,
  id : "usa",
  name : "United States",
  popY : 8571487476842,
  popZ : 1716024078740 }
   ]

我一直在搜索 stackoverflow 并发现了类似的问题,但无法使其正常工作。可能是因为我对php和php数组的理解。任何帮助表示赞赏。

4

4 回答 4

1

首先,您正在制作一系列不正确的语法:

[0] => {

其次,您正在寻找的结果是一组对象。这说;您将需要执行以下操作:

$array = array();

$array[0] = new StdObject;
$array[0] -> code = "EU27";
// other values added to object

$array[1] = new StdObject;
$array[1] -> code = "US";
// other values added to second object

$json = json_encode( $array );

或者,您也可以使用数组而不是对象:

$array = array();

$array[0] = array(
"code" => "EU27",
// other values
);

$array[1] = array(
"code" => "US",
// other values
);

$json = json_encode( $array );
于 2013-03-14T08:25:25.207 回答
0

您可以轻松实现这一目标。

首先以关联形式使用数组,方法如下:

 $a = [ 
 [
 'code'=>'EU27',
 'debtA' => 967576248244, .... etc
 ], 
 [
 'code'=>'Something',
 'debtA' => 874376347, .... etc
 ]
 ];

然后将内部数组转换为对象

 foreach($a as $k = $v)$a[$k] = (object)$v;

然后json编码它......悲剧的结束。
这是我尝试的示例:

 $a = [ 
 [
 'code'=>'EU27',
 'debtA' => 967576248244//, .... etc
 ], 
 [
 'code'=>'Something',
 'debtA' => 874376347//, .... etc
 ]
 ];

 foreach($a as $k => $v)$a[$k]=(object)$a[$k];
 echo json_encode($a);

这就是我得到的:

 [{"code":"EU27","debtA":967576248244},{"code":"Something","debtA":874376347}]

希望这可以帮助。

于 2013-03-14T08:27:28.967 回答
0

您获得的 JSON 字符串是 100% 有效的,并且与您构建的输入数组完全对应。json_encode()永远不会尝试做你认为理所当然的猜谜游戏:如果你有一个包含{PHP 的字符串,它将生成一个包含{- 它甚至不会考虑启动对象的可能性!

您显然需要以这种方式构建您的数组:

$input = array(
    array(
        'code' => 'EU27',
        'debtA' => '1739414366187',
        'debtB' => '8719158563660',
        'id' => 'eur',
        'name' => 'EU',
        'popY' => '460572909944',
        'popZ' => '2396933253407',
    ),
    array(
        'code' => 'US',
        'debtA' => '7810766144794',
        'debtB' => '10536751929567',
        'id' => 'usa',
        'name' => 'United States',
        'popY' => '8571487476842',
        'popZ' => '1716024078740',
    ),
);

$output = json_encode($input);

此外,必须引用键名——<a href="http://json.org/" rel="nofollow">这不是一个选项,因为它们是字符串:

字符串是零个或多个 Unicode 字符的序列,用双引号括起来,使用反斜杠转义。字符表示为单个字符串。字符串与 C 或 Java 字符串非常相似。

于 2013-03-14T08:31:26.440 回答
0

你想要的正确的json是

'             [
                  {
                   "code" : "EU27",
                   "debtA" : "1739414366187",
                   "debtB" : "8719158563660",
                   "id" : "eur",
                   "name" : "EU",
                   "popY" : "460572909944",
                   "popZ" : "2396933253407" },
                 {
                   "code" : "US",
                   "debtA" : "7810766144794",
                   "debtB" : "10536751929567",
                   "id" : "usa",
                   "name" : "United States",
                   "popY" : "8571487476842",
                   "popZ" : "1716024078740" }
                    ]

这个 json 对应于多维数组,应该是这样的:`

            Array
              (
                  [0] => Array
                      (
                          [code] => EU27
                          [debtA] => 1739414366187
                          [debtB] => 8719158563660
                          [id] => eur
                          [name] => EU
                          [popY] => 460572909944
                          [popZ] => 2396933253407
                      )

                  [1] => Array
                      (
                          [code] => US
                          [debtA] => 7810766144794
                          [debtB] => 10536751929567
                          [id] => usa
                          [name] => United States
                          [popY] => 8571487476842
                          [popZ] => 1716024078740
                      )

              )`          

为了更好地理解,请参见这里http://php.net/manual/en/function.json-encode.php '

于 2013-03-14T08:45:20.247 回答