栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

@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_depre
到的字符串转换成对象(
stdClass
)或数组:http://php.net/manual/en/function.json-
depre.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_depre($data);

但是使用

stdClass
它比使用PHP-Array要复杂得多,然后尝试一下(使用结合使用第二个参数
true
):

$manage = json_depre($data, true);

这样,您可以使用数组函数:http :
//php.net/manual/en/function.array.php

添加一个项目:

$manage = json_depre($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_depre($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_depre($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_enpre($manage));    fclose($id);}

我希望我理解你的问题。

祝好运。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号