2
I have a string:
{name:"Sara", daypart:"day", href:"/questions/ask", bg:"su", temp:"calor", realfeel:"hot", text:"cloudy"}
Basically I’m looking for a quick way to turn this into JSON, I mean, quote ("
) in each index (key), so that it becomes the valid JSON to then do json_decode()
Could you explain to me what you’re doing at
preg_replace
sff, and why "" in output– Miguel
comes from
json_encode
and not ofpreg_replace
,– Maicon Carraro
Without the
preg_replace
I can’t do this– Miguel
@Miguel the
preg_replace
in this case serves to put the"
in your keys– Maicon Carraro
Yes but you said "Although I don’t see the need to add " in the key, because you’ll be able to work with the JSON object the same way...", but I’m seeing that without the
preg_replace
wouldn’t. Anyway it worked, thank you so much again– Miguel
@Miguel using only the
json_encode($json);
didn’t work out?– KaduAmaral
@Kaduamaral the
json_encode
direct does not play"
in the keys– Maicon Carraro
Right, are you using the json that bind? If what ails you is the quotation marks, then Maicon’s answer is right for you. ;)
– KaduAmaral
Yes you did, but without the preg_replace (quotation marks) I wouldn’t, I wouldn’t print anything. I’m using php
– Miguel
Maicon
$json
is what he wants, not thejson_encode($json)
(this last one gives the "jsonzada" version of a string, not an object). And personal, he wants the quotes in the key otherwise JSON is invalid. Format specification requires quotation marks, right @Miguel?– bfavaretto
Exactly, I wanted it to stay JSON and then I could transform it into an array (
json_decode()
) (PHP) and work the data, which without the quotation marks would not work, would not be a valid JSON.– Miguel