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


71

我从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对象?

我需要将其作为一个对象,以便可以像“ Coords”一样添加一个额外的项目/元素/对象。


2
@ user2363025这是您的字符串,已转换为有效的JSON:pastebin.com/R16NVerw
Miro Markaravanes

@MiroMarkarian尽管JSON有效,但是您破坏了日期格式xD

JsonLint做到了我没有做到的。我张贴只是为了证明有效。以编程方式进行转换时,它不会发生
Miro Markaravanes

1
@YogeshSuthar谢谢,这是一个很好的链接!
user2363025 2013年

我看到您编辑了问题,告诉我:初始数据是要转换为PHP变量(数组/对象-stdClass)的JSON格式,添加数据并再次转换为JSON?注意:我编辑了答案。
Guilherme Nascimento 2013年

Answers:


129

@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到的字符串转换成对象(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-Array更复杂,然后尝试一下(使用结合使用第二个参数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);
}

希望我能理解您的问题。

祝好运。


感谢您抽出我的头发来说明为什么我要推送到另一个数组的数组显示为字符串而不是对象。
SleepNot 2014年

如果您不使用stdClass它并且无法使用简单json_decode功能,则通过将字符串(array)预先转换为我可以解决问题。
LatentDenis

20

要转换回有效的JSON字符串,可以使用json_decode()方法。

要将其转换回对象,请使用以下方法:

$jObj = json_decode($jsonString);

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

$jArr = json_decode($jsonString, true);

通过将您提到的字符串转换回任何一个字符串,您应该有一个有效的JSON字符串。为此,您应该执行以下操作:

  1. Coords数组中,"从对象的开头和结尾删除两个(双引号)。
  2. 数组中的对象以逗号分隔(,),因此请在Coords数组中的对象之间添加逗号。

您将拥有一个有效的JSON字符串。

这是您转换为有效字符串的JSON字符串:http : //pastebin.com/R16NVerw


是! 除了将类型转换为数组之外,我们可以使用的assoc参数json_encode。引用PHP文档:“当时TRUE,返回的对象将转换为关联数组。”
CPHPython


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.