如何保持真实'而不是' 1'在转换json_decode期间

时间:2016-09-21 16:50:23

标签: php json

我想将json文件转换为csv。在json,我有:

{
"fields": [
    {
        "_id": "identifier",
        "required": true,
        "size": 4,
        "type": "text"
    }
  ]
}

在数组中的json_encode之后我有[required] => 1我的问题是如何强制保持"必需"为true。我知道最简单的方法是检查if中的内容,然后分配字符串"true",但也许是另一种更有效的方法?

完整代码看起来像这样:

class csvConverter {

    public $fh;
    public $csv_file;
    public $json_file;

    public function __construct() {

    }

    public function fileOpen($str) {
        return fopen($str, 'w');
    }

    public function countCategories($json_obj) {
        $max = 0;
        foreach ($json_obj['sections'] as $section) {
            foreach ($section['fields'] as $field) {
                $count = count($field['categories']);
                if ($max < $count) {
                    $max = $count;
                }
            }
        }
        return $max;
    }

    public function convert() {
        try {
            $json = file_get_contents($this->json_file);
            $json_obj = json_decode($json, true);

            $max = $this->countCategories($json_obj);

            foreach ($json_obj['sections'] as $kk => $obj) {
                foreach ($obj['fields'] as $key => $field) {

                    $mainArray[$kk][$key] = array(
                        'Section id' => !empty($obj['_id']) ? $obj['_id'] : "",
                        'Section name' => !empty($obj['name']) ? $obj['name'] : "",
                        'Field id' => !empty($field['_id']) ? $field['_id'] : "",
                        'Name' => !empty($field['name']) ? $field['name'] : "",
                        'Descriprion' => !empty($field['description']) ? $field['description'] : "",
                        'Help' => !empty($field['help']) ? $field['help'] : "",
                        'Type' => !empty($field['type']) ? $field['type'] : "",
                        'Required' => !empty($field['required']) ? $field['required'] : "",
                        'Default' => !empty($field['default']) ? $field['default'] : "",
                        'Size' => !empty($field['size']) ? $field['size'] : "",
                    );

                    if (!empty($field['categories'])) {
                        foreach ($field['categories'] as $k => $v) {
                            $mainArray[$kk][$key]['categories_' . $k] = $v;
                        }
                    } else {
                        for ($i = 0; $i < $max; $i++) {
                            $mainArray[$kk][$key]['categories_' . $i] = "";
                        }
                    }
                }
            }

            $header .= implode(";", array_keys($mainArray[0][0])) . "\n";

            foreach ($mainArray as $element) {
                foreach ($element as $elem) {
                    $header .= implode(";", $elem) . "\n";
                }
            }
            fwrite($this->fh, $header . "\n");
        } catch (Exception $ex) {
            echo $ex->getMessage();
            die('Some problems !');
        }
    }

}

$csvObiect = new csvConverter();
$csvObiect->csv_file = 'file.csv';
$csvObiect->json_file = 'form.json';
$csvObiect->fh = fopen($csvObiect->csv_file, 'w');
$csvObiect->convert();

和json文件:

{
    "sections": [
        {
            "_id": "site",
            "fields": [
                {
                    "_id": "site-id",
                    "default": 2,
                    "name": "Site ID",
                    "required": true,
                    "size": 4,
                    "type": "number"
                }
            ],
            "name": "Site"
        },
        {
            "_id": "study-entry-1",
            "fields": [
                {
                    "_id": "identifier",
                    "description": "A unique, valid \"patient identifier\".",
                    "help": "Enter the *patient identifier* here.\nIt should be *no longer than* four characters.",
                    "name": "Identifier",
                    "required": true,
                    "size": 4,
                    "type": "text"
                },
                {
                    "_id": "date-of-study-entry",
                    "default": "22/01/2012",
                    "help": "DD/MM/YYYY",
                    "name": "Date of study entry",
                    "required": true,
                    "type": "date"
                },
                {
                    "_id": "injury",
                    "categories": [
                        "Neck",
                        "Head",
                        "Back"
                    ],
                    "default": "Neck",
                    "name": "Injury",
                    "type": "category"
                },
                {
                    "_id": "history",
                    "default": "Unknown",
                    "name": "History",
                    "type": "textarea"
                }
            ],
            "name": "Study entry"
        }
    ]
}

1 个答案:

答案 0 :(得分:3)

使用CSV无法实现。 CSV是 TEXT 格式。无法将元数据附加到该文本以区分“true-is-text”和“true-is-boolean”。

如果要将元数据存储在文件中,请不要使用CSV。使用具有更明确和STANDARD结构的东西,比如json,XML等......

CSV应该被视为“其他一切都失败”格式的最后手段,因为CSV不是标准。关于其中一些是如何工作的,只有一些基本的协议,然后就其余部分存在很多分歧,特别是:逃避元素。

这意味着,不,你不能让fgetcsv吸入一行csv并自动将单词true转换为布尔值。那不是csv的工作。