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

查看:114
本文介绍了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常量,并用+而不是管道(|)就像手册中演示的那样.


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天全站免登陆