在 PHP 中将字符串转换为 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17488207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string to JSON object in PHP
提问by user2363025
I have the following result from an sql query:
我从 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)"}
]
}
It is currently a string in php, is there an easy way to convert this to a JSON object (I know it's already in JSON form).
它目前是 php 中的一个字符串,是否有一种简单的方法可以将其转换为 JSON 对象(我知道它已经是 JSON 形式)。
I need it to be an object so I can add an extra item/element/object like what coords already is.
我需要它是一个对象,所以我可以添加一个额外的项目/元素/对象,就像坐标已经是什么一样。
EDIT: SORRY GUYS, I PASTED AN OLD/WRONG STRING!
编辑:对不起,伙计们,我粘贴了一个旧的/错误的字符串!
回答by Guilherme Nascimento
What @deceze said is correct, it seems that your JSON is malformed, try this:
@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)"
}]
}
Use json_decode
to convert String into Object (stdClass
) or array: http://php.net/manual/en/function.json-decode.php
使用json_decode
到的字符串转换成对象(stdClass
)或数组:http://php.net/manual/en/function.json-decode.php
[edited]
[编辑]
I did not understand what do you mean by "an official JSON object", but suppose you want to add content to json via PHP and then converts it right back to JSON?
我不明白“官方 JSON 对象”是什么意思,但假设您想通过 PHP 将内容添加到 json,然后将其转换回 JSON?
assuming you have the following variable:
假设您有以下变量:
$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)"}]}';
You should convert it to Object(stdClass):
您应该将其转换为Object(stdClass):
$manage = json_decode($data);
$manage = json_decode($data);
But working with stdClass
is more complicated than PHP-Array, then try this (use second param with true
):
但是使用stdClass
比 PHP-Array 更复杂,然后试试这个(使用第二个参数 with true
):
$manage = json_decode($data, true);
$manage = json_decode($data, true);
This way you can use array functions: http://php.net/manual/en/function.array.php
这样你就可以使用数组函数:http: //php.net/manual/en/function.array.php
adding an item:
添加一个项目:
$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);
remove first item:
删除第一项:
$manage = json_decode($data, true);
echo 'Before: <br>';
print_r($manage);
array_shift($manage['Coords']);
echo '<br>After: <br>';
print_r($manage);
any chance you want to save to json to a databaseor a file:
任何您想将 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);
}
I hope I have understood your question.
我希望我已经理解了你的问题。
Good luck.
祝你好运。
回答by Miro Markaravanes
To convert a valid JSON string back, you can use the json_decode()
method.
要将有效的 JSON 字符串转换回,您可以使用该json_decode()
方法。
To convert it back to an object use this method:
要将其转换回对象,请使用以下方法:
$jObj = json_decode($jsonString);
And to convert it to a associative array, set the second parameter to true
:
并将其转换为关联数组,请将第二个参数设置为true
:
$jArr = json_decode($jsonString, true);
By the way to convert your mentioned string back to either of those, you should have a valid JSON string. To achieve it, you should do the following:
顺便将您提到的字符串转换回其中之一,您应该有一个有效的 JSON 字符串。要实现它,您应该执行以下操作:
- In the
Coords
array, remove the two"
(double quote marks) from the start and end of the object. - The objects in an array are comma seprated (
,
), so add commas between the objects in theCoords
array..
- 在
Coords
数组中,"
从对象的开头和结尾删除两个(双引号)。 - 数组中的对象以逗号分隔 (
,
),因此在Coords
数组中的对象之间添加逗号。
And you will have a valid JSON String..
你将有一个有效的 JSON 字符串..
Here is your JSON String I converted to a valid one: http://pastebin.com/R16NVerw
这是我转换为有效字符串的 JSON 字符串:http: //pastebin.com/R16NVerw
回答by Nabeel Arshad
you can use this for example
例如,您可以使用它
$array = json_decode($string,true)
but validate the Json before. You can validate from http://jsonviewer.stack.hu/
但之前验证 Json。您可以从http://jsonviewer.stack.hu/进行验证