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

Posted

技术标签:

【中文标题】如何在 PHP 中将字符串转换为 JSON 对象【英文标题】:How to convert a string to JSON object in PHP 【发布时间】:2013-07-03 12:24:24 【问题描述】:

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

我需要它是一个对象,这样我就可以添加一个额外的项目/元素/对象,就像“坐标”已经是一样。

【问题讨论】:

@user2363025 这是您转换为有效 JSON 的字符串:pastebin.com/R16NVerw @MiroMarkarian 虽然 JSON 是有效的,但你打破了日期格式 xD JsonLint 做到了我没有做到的。我发布只是为了显示什么是有效的。以编程方式转换时,不会发生 @YogeshSuthar 谢谢,这是一个很好的链接! 我看到你编辑了问题,告诉我:初始数据是JSON格式你想转换为PHP变量(Array/Object - stdClass),添加数据并再次转换为JSON?注意:我编辑了我的答案。 【参考方案1】:

@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-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);

希望我能理解你的问题。

祝你好运。

【讨论】:

感谢您为我推到另一个数组的数组显示为字符串而不是对象的原因。 如果你不处理 stdClass 并且它不能使用简单的 json_decode 函数,通过将字符串转换为 (array) 预先为我做了诀窍。 对于我的情况,添加 true 标志解决了它。我只是在没有 true 参数的情况下传递数据,但没有得到正确的响应。谢谢。【参考方案2】:

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

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

$jObj = json_decode($jsonString);

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

$jArr = json_decode($jsonString, true);

顺便说一句,将您提到的字符串转换回其中任何一个,您应该有一个有效的 JSON 字符串。要实现它,您应该执行以下操作:

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

你将有一个有效的 JSON 字符串..

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

【讨论】:

是的!我们可以使用json_encodeassoc 参数来代替类型转换为数组。引用PHP docs:“当TRUE时,返回的对象会被转换成关联数组。”【参考方案3】:

你可以使用这个例子

$array = json_decode($string,true)

但要先验证 Json。您可以通过http://jsonviewer.stack.hu/进行验证

【讨论】:

【参考方案4】:

试试json_encode()

再看看Valid JSON

【讨论】:

以上是关于如何在 PHP 中将字符串转换为 JSON 对象的主要内容,如果未能解决你的问题,请参考以下文章