php里字符串转json对象,将一个字符串转换为JSON对象php

@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)将String转换为Array / 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)"}]}';

您应该将其转换为Array / Object(stdClass):

$ manage = json_decode($ data);

但是使用stdClass比PHP-Array更复杂,然后尝试这样做:

$ manage =(array)json_decode($ data);

添加一个项目:

$manage = (array) json_decode($data);

echo 'Before:
';

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 '
After:
';

print_r($manage);

删除第一项:

$manage = (array) json_decode($data);

echo 'Before:
';

print_r($manage);

array_shift($manage['Coords']);

echo '
After:
';

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 = (array) json_decode($data);

$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','w'))){

fwrite($id,json_encode($manage));

fclose($id);

}

我希望我了解你的问题。

祝你好运。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值