如何在PHP中将字符串转换为JSON对象

nfs0ujit  于 2022-12-01  发布在  PHP
关注(0)|答案(6)|浏览(214)

我从SQL查询得到以下结果:

{"Coords":[
    {"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},
    {"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},
    {"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},
    {"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},
    {"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"} 
    ]
}

它目前在PHP中是一个字符串。我知道它已经是JSON格式了,有没有简单的方法把它转换成JSON对象?
我需要它是一个对象,这样我就可以添加一个额外的项目/元素/对象,就像“坐标”已经是什么。

zf9nrax1

zf9nrax11#

@deceze说的没错,看来你的JSON格式不对,试试这个:

{
    "Coords": [{
        "Accuracy": "30",
        "Latitude": "53.2778273",
        "Longitude": "-9.0121648",
        "Timestamp": "Fri Jun 28 2013 11:43:57 GMT+0100 (IST)"
    }, {
        "Accuracy": "30",
        "Latitude": "53.2778273",
        "Longitude": "-9.0121648",
        "Timestamp": "Fri Jun 28 2013 11:43:57 GMT+0100 (IST)"
    }, {
        "Accuracy": "30",
        "Latitude": "53.2778273",
        "Longitude": "-9.0121648",
        "Timestamp": "Fri Jun 28 2013 11:43:57 GMT+0100 (IST)"
    }, {
        "Accuracy": "30",
        "Latitude": "53.2778339",
        "Longitude": "-9.0121466",
        "Timestamp": "Fri Jun 28 2013 11:45:54 GMT+0100 (IST)"
    }, {
        "Accuracy": "30",
        "Latitude": "53.2778159",
        "Longitude": "-9.0121201",
        "Timestamp": "Fri Jun 28 2013 11:45:58 GMT+0100 (IST)"
    }]
}

使用json_decode将String转换为Object(stdClass)或数组:http://php.net/manual/en/function.json-decode.php

[已编辑]

我不明白 “正式JSON对象” 是什么意思,但是假设您想通过PHP向json添加内容,然后将其转换回JSON?
假设您有以下变量:

$data = '{"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}]}';

您应该将其转换为 Object(stdClass):
$manage = json_decode($data);
但是使用stdClass比PHP数组复杂得多,那么试试这个(使用true的第二个参数):
$manage = json_decode($data, true);
这样就可以使用数组函数:http://php.net/manual/en/function.array.php

添加项目:

$manage = json_decode($data, true);

echo 'Before: <br>';
print_r($manage);

$manage['Coords'][] = Array(
    'Accuracy' => '90'
    'Latitude' => '53.277720488429026'
    'Longitude' => '-9.012038778269686'
    'Timestamp' => 'Fri Jul 05 2013 11:59:34 GMT+0100 (IST)'
);

echo '<br>After: <br>';
print_r($manage);

删除第一个项目:

$manage = json_decode($data, true);
echo 'Before: <br>';
print_r($manage);
array_shift($manage['Coords']);
echo '<br>After: <br>';
print_r($manage);

如果您想将json保存到 * 数据库 * 或 * 文件 *:

$data = '{"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}]}';

$manage = json_decode($data, true);

$manage['Coords'][] = Array(
    'Accuracy' => '90'
    'Latitude' => '53.277720488429026'
    'Longitude' => '-9.012038778269686'
    'Timestamp' => 'Fri Jul 05 2013 11:59:34 GMT+0100 (IST)'
);

if (($id = fopen('datafile.txt', 'wb'))) {
    fwrite($id, json_encode($manage));
    fclose($id);
}

我希望我已经理解了你的问题。
祝你好运

ccrfmcuu

ccrfmcuu2#

要将有效的JSON字符串转换回来,可以使用json_decode()方法。
若要将它转换回对象,请使用此方法:

$jObj = json_decode($jsonString);

若要将其转换为关联数组,请将第二个参数设置为true

$jArr = json_decode($jsonString, true);

顺便说一下,要将上述字符串转换回上述两种字符串中的任何一种,您应该有一个有效的JSON字符串。要实现这一点,您应该执行以下操作:
1.在Coords数组中,移除对象开头和结尾的两个"(双引号)。
1.数组中的对象以逗号分隔(,),因此请在Coords数组中的对象之间添加逗号。
您将得到一个有效的JSON字符串。
下面是我转换为有效字符串的JSON字符串:http://pastebin.com/R16NVerw

31moq8wy

31moq8wy3#

例如,可以使用以下命令

$array = json_decode($string,true)

但在此之前验证Json。您可以从http://jsonviewer.stack.hu/验证

rqcrx0a6

rqcrx0a64#

如果不传递第二个参数,或者传递false,json_decode()会将JSON解析为stdClass对象,因此您可以使用“-〉”箭头符号来访问对象属性。

<?php

// Store JSON data in a PHP variable
$json = '{"email":"john@doe.com"}';

$obj = json_decode($json, false);
print $obj->email;

?>

输出

john@doe.com
bfrts1fy

bfrts1fy5#

@Miro Markaravanes为我节省了大量的时间。在我的例子中,我在数据库中存储了一个类似于OP的JSON对象的字符串。当调用它时,字符串的开头和结尾都有双引号。
由于这一点,每次我试图使用json_decode它不会工作。
所以我做了以下几点:

$data = substr($data, 1);
    $data = substr($data, 0, -1);
    $data = json_decode($data);
6ie5vjzr

6ie5vjzr6#

尝试使用json_encode()
再看看Valid JSON

相关问题