来自 php 数组的意外 json 输出结构 [英] Unexpected json output structure from php array

查看:25
本文介绍了来自 php 数组的意外 json 输出结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换动态数据.如何从 PHP 获取此 JSON:

I am trying to convert a dynamic data. How to get this JSON from PHP:

/*JSON*/
{
   "122240cb-253c-4046-adcd-ae81266709a6": {
      "item": {
          "0": "3"
      }
   }
}

这就是我所做的,但它不起作用:

This is what I have done, but it's not working:

/*PHP*/

$json = array("122240cb-253c-4046-adcd-ae81266709a6"=> array(
    "item" => array($form_item)
));

echo json_encode($json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);

这是我得到的结果而不是上面的结果.

This is the result I am getting instead of the above.

{
"122240cb-253c-4046-adcd-ae81266709a6": {
    "0": {
        "item": 3
    }
}

推荐答案

首先,一些代码来演示需要调整的内容.

First, a bit of code to demonstrate what needs to be adjusted.

  1. item 键向上移动一级(从最低的子数组中移出)
  2. 引用包裹您的 $form_item 值以使其成为字符串.
  1. Move the item key up one level (out of the lowest subarray)
  2. Quote-wrap your $form_item value to make it a string.

代码:(演示)

$form_item = 3;
$original_json = array("122240cb-253c-4046-adcd-ae81266709a6"=> array(
    array("item" => $form_item)
));
echo json_encode($original_json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);

echo "\n---\n";

$form_item = "3";
$desired_json = array("122240cb-253c-4046-adcd-ae81266709a6"=> array(
    "item" => array($form_item)
));
echo json_encode($desired_json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);

输出:

{
    "122240cb-253c-4046-adcd-ae81266709a6": {
        "0": {
            "item": 3
        }
    }
}
---
{
    "122240cb-253c-4046-adcd-ae81266709a6": {
        "item": {
            "0": "3"
        }
    }
}

<小时>

现在到更有趣的部分,乍一看让我绊倒...

您使用的选项参数的语法是我以前从未见过的,并且在 json_encode() 文档页面.您列出了多个 json 常量 并用 + 分隔它们code> 而不是像手册演示的管道 (|).


Now onto the more interesting part that tripped me up at first glance...

You are using a syntax with the options parameters that I've never seen before and is not mentioned on the json_encode() documentation page. You are listing multiple json constants and separating them with + instead of the pipes (|) like the manual demonstrates.

为了解释为什么这是有效的语法,我必须表达幕后"发生的事情.

To explain why this is valid syntax, I must express what is happening "behind the scenes".

常量实际上是位掩码".每个常量都分配了一个编号.

The constants are actually "bitmasks". Each constant is assigned a number.

JSON_HEX_TAG => 1
JSON_HEX_AMP => 2
JSON_HEX_APOS => 4
JSON_HEX_QUOT => 8
JSON_FORCE_OBJECT => 16
JSON_NUMERIC_CHECK => 32
JSON_UNESCAPED_SLASHES => 64
JSON_PRETTY_PRINT => 128
JSON_UNESCAPED_UNICODE => 256
JSON_PARTIAL_OUTPUT_ON_ERROR => 512
JSON_PRESERVE_ZERO_FRACTION => 1024

JSON_HEX_TAG => 1
JSON_HEX_AMP => 2
JSON_HEX_APOS => 4
JSON_HEX_QUOT => 8
JSON_FORCE_OBJECT => 16
JSON_NUMERIC_CHECK => 32
JSON_UNESCAPED_SLASHES => 64
JSON_PRETTY_PRINT => 128
JSON_UNESCAPED_UNICODE => 256
JSON_PARTIAL_OUTPUT_ON_ERROR => 512
JSON_PRESERVE_ZERO_FRACTION => 1024

你看,这些数字不是随意分配的;每个累进数字故意是前一个数字的两倍.为什么?因为如果你敢于列出多个 options,你可以写一个代表任意两个或多个常量之和的单个数字,你永远不会意外地成为值冲突的牺牲品.

You see, these numbers are not arbitrarily assigned; each progressive number is deliberately twice the previous number. Why? Because if you dare to list multiple options, you can write a single number that represents the sum of any two or more constants and you will never accidentally fall prey to a value collision.

这是什么意思?以下所有表达式都产生相同的输出:

What does this mean? All of the following expressions produce the same output:

echo json_encode($json, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT);
echo json_encode($json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);
echo json_encode($json, 16 + 128);
echo json_encode($json, 144);

想要证明?(演示)

这篇关于来自 php 数组的意外 json 输出结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆