JSON won't decode from string in PHP - php

Good evening,
This is probably a stupid question but I've been fiddling with this for a while now. I have a string coming from an AJAX call. To inspect the actual string that gets sent to my PHP from some JavaScript I put it into the result and spit it out in the front end. The string looks like this:
count: "[{\"cartKey\":\"d9d4f495e875a2e075a1a4a6e1b9770f\",\"qty\":\"3\"},
{\"cartKey\":\"67c6a1e7ce56d3d6fa748ab6d9af3fd7\",\"qty\":\"2\"},
{\"cartKey\":\"f7177163c833dff4b38fc8d2872f1ec6\",\"qty\":\"32\"}]"
So! My problem is getting this to be an actual PHP array. If I do this:
$result['count'] = json_decode($updates, true);
inside my PHP, the result is 0.
Ignore the count name on the result. I'm just trying to turn the above string into an array of objects I can use in PHP rather than a JSON string.
Thanks in advance!

I'm a birk.
The actual value of $updates was null, I think.
I have just solved my problem by not calling JSON.stringify on the data sent from the AJAX call.
The value now comes through and is actually decoded as if by magic within the PHP script.
Thanks for taking a look.
Ash

Related

Get specific value of array object

I have an array object below
33 => 'a:2:{
s :8:"latitude";
s:10:"39.3600586";
s:9:"longitude";
s:18:"-84.30993899999999";
}'
And here is the variable I'm using to get the value of that object
$events_location = $entries[1]['33'];
Is there a way to get just the value of either the latitude or longitude instead of everything in the single quotes?
Thanks!
What you have here is a serialized string. Unserialize it to access the key in the array:
$events_location = unserialize($entries[1]['33']);
echo $events_location['longitude'];
This should be a comment but its a bit long.
The string you have shown us looks vaguely like part of a serialized php entity. But it's not. If you try to unserialize this you'll get an error. The underlying data appears to be a coordinate - but even ignoring the syntactic errors, the semantics of the structure are wrong.
Please check the source you transcribed this from. If you have not copied the original content here please amend your question.
If you have transcibed it correctly then go speak to whoever supplied you with this data and ask them to fix it.

parsing text / json with php

{"email":"test#example.com","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}
I'm having trouble parsing this string. It's from Sendgrid's Event API, it seems to be "almost" JSON, but json_decode won't work. My goal is to get the data into an array, then into a MySQL table. I'm not asking anyone to write the code for me, just point me toward the correct method. Do I use the explode function, then json_decode?.
(I'm slowly teaching myself PHP, sorry if the question is not clear)
I think you're not using the json_decode function with $assoc = true, so you're getting an object rather than an array
$json =
'{"email":"test#example.com","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}';
var_dump(json_decode($json, true));
You'll get the result as array;

Can PHP easily parse JSON objects into PHP objects?

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

What datatype is this? is it wordpress specific?

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().

exported json as string (not array)

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.

Categories