I try to serialize my data with PHP. Unfortunaly, the serialize() function returns a wrong value.
String to be serialized:
{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}
What serialize() returns on my server:
serialize($code);
s:204:"{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}";
What should be returned (https://duzun.me/playground/serialize):
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
You need to json_decode it first to get the wanted result:
When you use the boolean switch as second parameter in json_decode it will be an array instead of an object.
$serialized = serialize(json_decode($inputString, true));
echo $serialized;
// output:
// a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
The site you're using isn't clear about what it's doing, but it appears to be treating the string as JSON and decoding to an array before serializing it as PHP. If you want to replicate this, you can use:
$str = '{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}';
echo serialize(json_decode($str, true));
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
As pointed out in the comments, unless there's a specific reason you need serialized PHP, then just stick with the serialized JSON string you already have - it'll be both more readable and portable.
Related
I have an array with multiple items in it one of which is an already encoded json string. I'm wanting to json encode the whole array but in doing so it re-json_encodes the json and adds slashes to it. The only way I've found to fix this is to json_decode the value and then encode the whole array. I feel like this is a waste of resources though and I feel like there has to be a better way. Is doing it that way the best possible way?
Here's a sample of the array I'm trying to json_encode.
$arr = array();
$arr["var1"] = '{"test":"test"}';
$arr["var2"] = 'foo';
$arr["var3"] = 'bar';
If I don't decode the var1 first and I just encode the whole array I get a result like so
{"var1":"{\"test\":\"test\"}","var2":"foo","var3":"bar"}
Notice the slashes in the json object.
json_encode() returns a string containing the json representation of a value.
In the example, a php string is passed as one element of the array '{"test":"test"}', thus json_encode() is encoding it appropriately into json format, with escaped quotes "{\"test\":\"test\"}".
If decoding nested json is a very resource heavy task, a workaround is to use a placeholder instead of the value, {"var1":"PLACEHOLDER","var2":"foo","var3":"bar"}, and then using str_replace() to replace it.
However, simply decoding it as you described is probably a cleaner solution, if its not resource heavy.
I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.
I have this string stored in a database column:
O:8:"stdClass":2:{s:4:"type";s:5:"Point";s:11:"coordinates";a:2:{i:0;d:-23.5663719;i:1;d:-46.65284157;}}
And I just want the "coordinates" in the middle:
-23.5663719,-46.65284157
There is a easy way to parse it in PHP to get this value?
I tried json_decode but it didn't work.
Thanks.
It's not json encoded, but serialized using serialize().
So what you have to do is:
$o = unserialize($str); // where $str is a string fetched from DB
var_dump($o->coordinates);
References:
http://www.php.net/manual/en/function.serialize.php
http://www.php.net/manual/en/function.unserialize.php
I have a string, more specifically, this one:
a:16:{s:9:"pseudonym";O:16:"SimpleXMLElement":0:{}s:14:"parallel_title";O:16:"SimpleXMLElement":0:{}s:9:"title_var";O:16:"SimpleXMLElement":0:{}s:6:"series";O:16:"SimpleXMLElement":0:{}s:9:"vol_title";O:16:"SimpleXMLElement":0:{}s:9:"reference";O:16:"SimpleXMLElement":0:{}s:10:"bound_with";O:16:"SimpleXMLElement":0:{}s:15:"general_remarks";O:16:"SimpleXMLElement":0:{}s:6:"copies";O:16:"SimpleXMLElement":1:{i:0;s:1:"1";}s:11:"remarks_BPH";O:16:"SimpleXMLElement":0:{}s:3:"ICN";O:16:"SimpleXMLElement":1:{i:0;s:4:"neen";}s:10:"provenance";O:16:"SimpleXMLElement":0:{}s:7:"binding";O:16:"SimpleXMLElement":0:{}s:10:"size_hxwxd";O:16:"SimpleXMLElement":0:{}s:6:"BookID";O:16:"SimpleXMLElement":1:{i:0;s:4:"6271";}s:5:"repro";O:16:"SimpleXMLElement":0:{}}
Is it possible to parse this string somehow? I need to display the keys and values in a list. I tried to use json_decode but it doesn't return anything, even with the second parameter set to true:
json_decode($string,true);
It's not JSON, it's serialized PHP. Use unserialize().
It's serialize object
Read more on PHP website
I have my data stored in a JSON string like these...
a:1:{s:15:"s2member_level1";s:1:"1";}
How can i read this values in mysql?
I need to know if the value "s2member_level1" is 1.
Thanks!!!
That's not JSON but a string resulted from calling serialize() in PHP. You cannot parse it easily in MySQL. If you can use PHP, use the unserialize function:
$obj = unserialize($data_from_mysql);
if ($obj['s2member_level1'] == 1) {
// more code here
}
You can convert data to JSON in PHP using the json_encode function. In a similar way, you construct an object from a JSON string using json_decode.
#Lekensteyn is correct, but you could do a like statement, although its performance would most likely be very poor. My true answer is to change how you store this information to take advantages of best performing queries.
select * from table
where column like '%s:15:"s2member_level1";s:1:"1";%';
#Lekensteyn is right about the type of this particular String, but for others, PHP has json_decode which you can use to convert a JSON object to a PHP object. It would be considerably more difficult to read such an object using MySQL only.
This is no json, but serialized data. It was probably serialized with the 'serialize' function of PHP. Try:
print_r(unserialize('a:1:{s:15:"s2member_level1";s:1:"1";}'));
... to unserialize it.