More or less, my question is as above.
I have A lot of data i am going to serialize and send to the server. With that said, is there a PHP function to parse it into PHP-objects to manipulate on the Serverside?
My thought is yes due to the dynamic nature of PHP, but i wasnt sure what it would be.
You essentially answered your own question. From the PHP manual entry for json_decode:
json_decode
Takes a JSON encoded string and converts it into a PHP variable.
That said, you'll obviously want to do all the requisite error checking and such.
You can also use json_decode with the true parameter to effectively convert it into to a PHP array like so: $var = json_decode($object,true);
Related
I have this value under Items in my DB:
a:1:{i:0;a:9:{s:12:"model_number";s:10:"TT1-W";s:5:"price";s:4:"3810";s:10:"unit_price";d:3135.6300000000001091393642127513885498046875;s:8:"id_price";d:3810;s:9:"sales_tax";d:290.3700000000000045474735088646411895751953125;s:5:"sales";d:3084.6300000000001091393642127513885498046875;s:7:"service";s:2:"51";s:7:"freight";s:3:"384";s:13:"co_cat";s:3:"X4";}}
Making it more reader-friendly:
a:1:
{
i:0;
a:9:
{
s:12:"model_number";
s:10:"TT1-W";
s:5:"price";
s:4:"3810";
s:10:"unit_price";
d:3135.6300000000001091393642127513885498046875;
s:8:"id_price";
d:3810;
s:9:"sales_tax";
d:290.3700000000000045474735088646411895751953125;
s:5:"sales";
d:3084.6300000000001091393642127513885498046875;
s:7:"service";
s:2:"51";
s:7:"freight";
s:3:"384";
s:13:"co_cat";
s:3:"X4";
}
}
I am unable to find out how to decode this string since it can not seem to find reference to it in the php code that displays it on the page. It looks to me to be JSON but i can not seem to find a "standard" format for the above in order to start figuring out where it starts and where it ends.
I am needing this to be decoding using ASP.net. But then again, i need to figure out what it is before i can start decoding it!
Any help to what it is would be great!
Try with unserialize:
function.unserialize
EDIT: If you can use C# libraries:
How to unserialize PHP Serialized array/variable/class and return suitable object in C#
EDIT2:
Visual Studio Tip: Three ways to use C# within a VB.NET project
EDIT3:
i need to figure out what it is
It's standard PHP-solution to store (and restore) arrays and objects (and other types, see manual) in strings.
That appears to be PHP's serialization methodology. You just need to use PHP's unserialize() on it.
This looks a serialized object. PHP's unserialize is probably what you want:
unserialize() takes a single serialized variable and converts it back
into a PHP value.
There is no built in way to turn that into an ASP.Net object, but it is a regular format, so you can build your own parser to create a simple dictionary representation of the attributes of that particular structure.
But if you're trying to de-serialize a PHP object in ASP.Net you're probably doing something wrong!
I have some json that i decode using json decode,
however I have floats in the json file, like "58.939020934234" and json_decode parses it as a float (just like it should) however on large numbers its chopping off the ends ( again, like it should ) however this is really bad and I need to find a way to FORCE json_decode to parse EVERYTHING as a string.
I read the documentation and the forth parameter is a flag and we can pass the const JSON_BIGINT_AS_STRING which I think is what I need, however when I try this i get an error saying json_decode does not accept 4 arguments! I'm running php5.
Does anybody know of another I could force this json to be parsed as string?
The options parameter was added in PHP 5.4, which isn't stable yet. This from the changelog on the man page for json_decode:
5.4.0 The options parameter was added.
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.
Hey can anyone tell me what datatype this is? it wont parse as i've stripped out sensitive data. Am I correct in thinking its json?
a:7:{s:12:"competitions";a:893:{i:1;s:10:"Ersie";i:5;s:19:"General
News"1510126584;s:0:"";i:1019;s:0:"";i:8284;s:0:"";i:191016;s:0:"";i:284;s:0:"";i:91019;s:0:"";i:81863;s:0:"";i:1563;s:0:"";i:710138;s:0:"";i:101333;s:0:"";i:33430;s:0:"";i:10224;s:0:"";i:10430;s:0:"";i:13430;s:0:"";i:375;s:0:"";i:72107;s:0:"";i:11372;s:0:"";i:181372;s:0:"";i:1885;s:0:"";i:107155;s:0:"";i:10284;s:0:"";i:8206;s:0:"";i:8101316;s:0:"";i:1913;s:0:"";i:206;s:0:"";i:772138;s:0:"";i:72284;s:0:"";i:1672155;s:0:"";i:7101663;s:0:"";i:891013;s:0:"";i:101843;s:0:"";i:1107;s:0:"";i:1072;s:0:"";i:830;s:0:"";i:7284;s:0:"";i:8101333;s:0:"";i:13372;s:0:"";i:1570;s:0:"";i:2543;s:0:"";i:91316;s:0:"";i:385;s:0:"";i:8385;s:0:"";i:2843;s:0:"";i:695;s:0:"";i:1970;s:0:"";i:1661;s:0:"";i:18206;s:0:"";i:33155;s:0:"";i:787;s:0:"";i:8117;s:0:"";i:1943;s:0:"";i:3043;s:0:"";i:872239;s:0:"";i:872155;s:0:"";i:910104;s:0:"";i:8125;s:0:"";i:239;s:0:"";i:8428;s:0:"";i:1382;s:0:"";i:87293;s:0:"";i:95385;s:0:"";i:11672;s:0:"";i:92572;s:0:"";i:828;s:0:"";i:8239;s:0:"";i:640;s:0:"";i:87155241;s:0:"";i:26155241;s:0:"";i:87158241;s:0:"";i:69158241;s:0:"";i:82543;s:0:"";i:193372;s:0:"";i:90163241;s:0:"";i:163372;s:0:"";i:1016107;s:0:"";i:86372;s:0:"";i:87163241;s:0:"";i:155162241;s:0:"";i:713121;s:0:"";i:2690241;s:0:"";i:895430;s:0:"";i:-403549467;s:0:"";i:2060490689;s:0:"";i:7181924;s:0:"";i:157158241;s:0:"";i:87295;s:0:"";i:71672430;s:0:"";i:1372430;s:0:"";i:9430;s:0:"";i:897;s:0:"";i:242;s:0:"";i:87162241;s:0:"";i:161863;s:0:"";i:1824107;s:0:"";i:26165241;s:0:"";i:7891316;s:0:"";i:81670;s:0:"";i:43107;s:0:"";i:710121;s:0:"";i:172283;s:0:"";i:8790241;s:0:"";i:253372;s:0:"";i:71863;s:0:"";i:26157241;s:0:"";i:8393;s:0:"";i:91824;s:0:"";i:826;s:0:"";i:63640;s:0:"";i:26163241;s:0:"";i:13121;s:0:"";i:82574;s:0:"";i:165241;s:0:"";i:87157241;s:0:"";i:2687241;s:0:"";i:26162241;s:0:"";i:199;s:0:"";i:18910;s:0:"";i:162165241;s:0:"";i:751430;s:0:"";i:16125;s:0:"";i:81893;s:0:"";i:79101316;s:0:"";i:81693;s:0:"";i:8913119;s:0:"";i:7818;s:0:"";i:72223;s:0:"";i:781072;s:0:"";i:972107;s:0:"";i:26241;s:0:"";i:90158241;s:0:"";i:87071;s:0:"";i:816125;s:0:"";i:72155430;s:0:"";i:72109;s:0:"";i:8106;s:0:"";i:181;s:0:"";i:8126;s:0:"";i:897293;s:0:"";i:187;s:0:"";i:101318;s:0:"";i:7166372;s:0:"";i:13216;s:0:"";i:101367;s:0:"";i:91149;s:0:"";i:781633;s:0:"";i:7101363;s:0:"";i:872430;s:0:"";i:1657;s:0:"";i:215;s:0:"";i:1025;s:0:"";i:7136372;s:0:"";i:1314;s:0:"";i:101319;s:0:"";i:1372344;s:0:"";i:1372573;s:0:"";i:1672430;s:0:"";i:872109;s:0:"";i:972430;s:0:"";i:193372430;s:0:"";i:13171;s:0:"";i:172430;s:0:"";i:6372430;s:0:"";i:772430;s:0:"";i:1372155430;s:0:"";i:71372;s:0:"";i:887;s:0:"";i:78933344;s:0:"";i:8131672;s:0:"";i:131672;s:0:"";i:365;s:0:"";i:91172;s:0:"";i:772155;s:0:"";i:972155;s:0:"";i:137072;s:0:"";i:729;s:0:"";i:781672;s:0:"";i:891172;s:0:"";i:924;s:0:"";i:796372;s:0:"";i:94372;s:0:"";i:284385;s:0:"";i:72344;s:0:"";i:71372121;s:0:"";i:78972;s:0:"";i:136372;s:0:"";i:713211;s:0:"";i:17972;s:0:"";i:101872;s:0:"";i:17872;s:0:"";i:84370;s:0:"";i:71633;s:0:"";i:172107;s:0:"";i:833732;s:0:"";i:18101318;s:0:"";i:816155;s:0:"";i:83343;s:0:"";i:43155;s:0:"";i:438;s:0:"";i:104363;s:0:"";i:961;s:0:"";i:843732;s:0:"";i:78107;s:0:"";i:13344;s:0:"";i:72393;s:0:"";i:83363;s:0:"";i:113121;s:0:"";i:863121;s:0:"";i:8131643;s:0:"";i:518;s:0:"";i:1789;s:0:"";i:61107;s:0:"";i:891619;s:0:"";i:43214;s:0:"";i:63121;s:0:"";i:7136430;s:0:"";i:81865;s:0:"";i:728430;s:0:"";s:11:"10245074125";s:0:"";i:63162;s:0:"";i:393;s:0:"";i:89732;s:0:"";i:2633430;s:0:"";i:157283430;s:0:"";}s:8:"lastpost";i:1281041491;s:7:"default";s:19:"General
News";s:11:"autopublish";s:2:"on";s:5:"draft";N;s:6:"poster";s:1:"1";s:6:"suffix";s:0:"";}
This is a serialized PHP array. Essentially, it's just a string that represents a PHP array (the a at the beginning marks it as an array).
You can get the PHP array back out of it by passing the string to the PHP function unserialize().
That is serialized data - not Wordpress specific.
Take a look into serialize(), unserialize() and OOP serialization: __sleep()/__wakeUp().
Some have see that code :
<?php
echo stripslashes(json_encode(glob("photos-".$_GET["folder"].'/*.jpg')));
?>
it echo a really nice perfect string like that :
["photos-animaux/ani-01.jpg","photos-animaux/ani-02.jpg","photos-animaux/ani-02b.jpg","photos-animaux/ani-03.jpg","photos-animaux/ani-04.jpg","photos-animaux/ani-05.jpg","photos-animaux/ani-06.jpg","photos-animaux/ani-07.jpg","photos-animaux/ani-08.jpg","photos-animaux/ani-09.jpg","photos-animaux/ani-10.jpg","photos-animaux/ani-11.jpg","photos-animaux/ani-12.jpg","photos-animaux/ani-13.jpg","photos-animaux/ani-14.jpg"]
With json encode it shoul send a array so variable[0] should = to photos-animaux/ani-01.jpg
NOW it is only the fisrt caracter of the string..
how a array get converted to string, and how in javascipt to convert string to array to be able to get value [0] [1] etc..
the question is WHEN or WHY the perfect array get converted to string anyway ?
Using JSON.parse(), from the library available here, is preferable to using eval() since this only reads JSON strings and hence avoids the inherant security risks associated with eval().
In order to parse a JSON-encoded string into actual javascript objects, you need to eval() the data returned in order for javascript to execute against the data:
var data = eval(json_string)
Keep in mind that you should only ever run eval on a string when you trust the source and are sure that there is no possibility of a script injection attack, since javascript will run everything present in the string.
Use one of the JSON libraries listed at the bottom of http://json.org/
WHEN or WHY
If I understood correctly, the answer to both is json_encode(): check out the documentation. Converting a variable to a string (often called serialization or flattening) is a quite common operation.