php 以正确的方式创建 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3281354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:13:32  来源:igfitidea点击:

Create JSON-object the correct way

phpjson

提问by Mark Denn

I am trying to create an JSON object out of a PHP array. The array looks like this:

我正在尝试从 PHP 数组中创建一个 JSON 对象。该数组如下所示:

$post_data = array('item_type_id' => $item_type,
    'string_key' => $string_key,
    'string_value' => $string_value,
    'string_extra' => $string_extra,
    'is_public' => $public,
    'is_public_for_contacts' => $public_contacts);

The code to encode the JSON look like this:

编码 JSON 的代码如下所示:

$post_data = json_encode($post_data);

The JSON file is supposed to look like this in the end:

JSON 文件最终应该是这样的:

{
    "item": {
        "is_public_for_contacts": false,
        "string_extra": "100000583627394",
        "string_value": "value",
        "string_key": "key",
        "is_public": true,
        "item_type_id": 4,
        "numeric_extra": 0
    }
} 

How can I encapsulate the created JSON code in the "item": { JSON CODE HERE }.

如何将创建的 JSON 代码封装在“项目”中:{ JSON CODE HERE }。

回答by Cristian

Usually, you would do something like this:

通常,你会做这样的事情:

$post_data = json_encode(array('item' => $post_data));

But, as it seems you want the output to be with "{}", you better make sure to force json_encode()to encode as object, by passing the JSON_FORCE_OBJECTconstant.

但是,由于您似乎希望输出带有“ {}”,因此您最好确保json_encode()通过传递JSON_FORCE_OBJECT常量来强制将其编码为对象。

$post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);

"{}" brackets specify an object and "[]" are used for arrays according to JSON specification.

" {}" 括号指定一个对象," []" 用于根据 JSON 规范的数组。

回答by theDmi

Although the other answers posted here work, I find the following approach more natural:

尽管此处发布的其他答案有效,但我发现以下方法更自然:

$obj = (object) [
    'aString' => 'some string',
    'anArray' => [ 1, 2, 3 ]
];

echo json_encode($obj);

回答by zeusstl

You just need another layer in your php array:

你只需要你的 php 数组中的另一个层:

$post_data = array(
  'item' => array(
    'item_type_id' => $item_type,
    'string_key' => $string_key,
    'string_value' => $string_value,
    'string_extra' => $string_extra,
    'is_public' => $public,
   'is_public_for_contacts' => $public_contacts
  )
);

echo json_encode($post_data);

回答by Alauddin Afif Cassandra

$post_data = [
  "item" => [
    'item_type_id' => $item_type,
    'string_key' => $string_key,
    'string_value' => $string_value,
    'string_extra' => $string_extra,
    'is_public' => $public,
    'is_public_for_contacts' => $public_contacts
  ]
];

$post_data = json_encode(post_data);
$post_data = json_decode(post_data);
return $post_data;

回答by Mitesh

You could json encode a generic object.

您可以对通用对象进行 json 编码。

$post_data = new stdClass();
$post_data->item = new stdClass();
$post_data->item->item_type_id = $item_type;
$post_data->item->string_key = $string_key;
$post_data->item->string_value = $string_value;
$post_data->item->string_extra = $string_extra;
$post_data->item->is_public = $public;
$post_data->item->is_public_for_contacts = $public_contacts;
echo json_encode($post_data);