【问题标题】:How to create a JSON object如何创建 JSON 对象
【发布时间】:2011-03-17 22:01:53
【问题描述】:

我正在尝试从 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);

对 JSON 进行编码的代码如下所示:

$post_data = json_encode($post_data);

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
    }
}

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

【问题讨论】:

    标签: php json


    【解决方案1】:

    通常,你会这样做:

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

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

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

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

    【讨论】:

    • 我会在json_encode($arr, JSON_FORCE_OBJECT)中添加JSON_FORCE_OBJECT
    • 这是正确的吗? $post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);
    • 也许这对某人有帮助 - jsonwrapper boutell.com/scripts/jsonwrapper.html json_(en|de)code 用于 PHP 的早期版本
    • 如果我有一个嵌套在$post_data 内的数组怎么办。这也会使它们成为对象,对吗?
    • echo json_encode(array('item' =>$post_data));将创建以下 JSON 结构:对象、数组、对象。或者: { [ { 这正是我想要的,将 MySQL JSON 响应导入 iOS 应用程序 :-) 谢谢克里斯蒂安!!!
    【解决方案2】:

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

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

    【讨论】:

    • 这个反应太好了。此外,当您无法准确控制何时对对象进行编码或想要对对象数组进行编码时:JSON_FORCE_OBJECT 响应不起作用。另一方面更具可读性。谢谢!
    • 如果您正在寻找以对象开头并包含数组的编码,这就是您的答案。
    【解决方案3】:

    你只需要在你的 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);
    

    【讨论】:

      【解决方案4】:

      您可以对通用对象进行 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);
      

      【讨论】:

        【解决方案5】:
        $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;
        

        【讨论】:

          猜你喜欢
          • 2019-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-25
          • 2011-05-06
          • 2016-03-26
          • 2016-01-23
          相关资源
          最近更新 更多