Convert a string to JSON object php

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

I have the following result from an sql query:

{"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).

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!

回答1:

What @deceze said is correct, it seems that your JSON is malformed, try this:

{     "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($string) to convert String into Array/Object (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?

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 Array/Object (stdClass):

$manage = json_decode($data);

But working with stdClass is more complicated than PHP-Array, then try this:

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

this way you can use array functions: http://php.net/manual/en/function.array.php

adding an item:

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

remove first item:

$manage = (array) json_decode($data); echo 'Before: 
'; print_r($manage); array_shift($manage['Coords']); echo '
After:
'; print_r($manage);

any chance you want to save to json to a database or a file:

$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); } 

I hope I have understood your question.

Good luck.



回答2:

To convert a valid JSON string back, you can use the json_decode() method.

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:

$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:

  1. In the Coords array, remove the two " (double quote marks) from the start and end of the object.
  2. The objects in an array are comma seprated (,), so add commas between the objects in the Coords array..

And you will have a valid JSON String..

Here is your JSON String I converted to a valid one: http://pastebin.com/R16NVerw



回答3:

you can use

$array = json_decode($sting,true) 

but validate the Json before. You can validate from http://jsonviewer.stack.hu/



回答4:

Try with json_encode().

And look again Valid JSON



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!