在PHP的JSON对象的开头和结尾添加单引号

时间:2018-08-04 14:23:13

标签: php arrays json string

我有一个JSON对象:

{"status":"true","result":{
"1":{"nim":"001","nama":"LEROY","prodi":"MI","jurusan":"TEKNOLOGI INFORMASI","id_kelas":"1"},
"2":{"nim":"002","nama":"JENKINS","prodi":"MI","jurusan":"TEKNOLOGI INFORMASI","id_kelas":"1"}},
"message":"Data Berhasil Ditambahkan"}

我想在JSON对象的开头和结尾添加一个单引号(')。就像:

'{"status":"true","result":{
"1":{"nim":"001","nama":"LEROY","prodi":"MI","jurusan":"TEKNOLOGI INFORMASI","id_kelas":"1"},
"2":{"nim":"002","nama":"JENKINS","prodi":"MI","jurusan":"TEKNOLOGI INFORMASI","id_kelas":"1"}},
"message":"Data Berhasil Ditambahkan"}'

我一直在尝试使用concat命令:

"'".<json object above>."'"

但是看来这不是我所需要的,因为它导致对象的类型(字符串)不同。

无论如何,我需要帮助,任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

嗯...它对我有用。我将您的原始字符串复制/粘贴到文件中(另存为“ / home / me / jo”)

{"status":"true","result":{
"1":{"nim":"001","nama":"LEROY","prodi":"MI","jurusan":"TEKNOLOGI INFORMASI","id_kelas":"1"},
"2":{"nim":"002","nama":"JENKINS","prodi":"MI","jurusan":"TEKNOLOGI INFORMASI","id_kelas":"1"}},
"message":"Data Berhasil Ditambahkan"}

然后

<?php

$json=file_get_contents("jo");

$json_object=json_decode($json);

print_r($json_object);

?>

给我

stdClass Object
(
    [status] => true
    [result] => stdClass Object
        (
            [1] => stdClass Object
                (
                    [nim] => 001
                    [nama] => LEROY
                    [prodi] => MI
                    [jurusan] => TEKNOLOGI INFORMASI
                    [id_kelas] => 1
                )

            [2] => stdClass Object
                (
                    [nim] => 002
                    [nama] => JENKINS
                    [prodi] => MI
                    [jurusan] => TEKNOLOGI INFORMASI
                    [id_kelas] => 1
                )

        )

    [message] => Data Berhasil Ditambahkan
)

那到底是什么问题?