如何使用php在txt文件中保存嵌套的json

时间:2013-09-26 06:45:25

标签: php json coding-style nested

如何快速轻松地保存嵌套在php中的JSON。我对嵌套的意思是:

嵌套json:

{
    "key1":"value",
    "key2":"value",
    "key3":"value",
    "key4":{
        "key1":"value",
        "key2":"value",
        "key3":"value"
    },
    "key5":{
        "key1":"value",
        "key2":"value",
        "key3":"value"
    }
}

json plain in one line (当我只使用echo json_encode($ array))

{"key1":"value","key2":"value","key3":"value","key4":{"key1":"value","key2":"value","key3":"value"},"key5":{"key1":"value","key2":"value","key3":"value"}}

我想要的原因是json必须易于人类阅读。当所有事情都在一条线上被打乱时,这无济于事。

1 个答案:

答案 0 :(得分:1)

json_encode使用正确的$options参数(PHP> = 5.4)

echo json_encode($object, JSON_PRETTY_PRINT);