prevent php from parsing floats as floats in json_decode - php

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.

Related

How do I properly json_encode ["number" => 12, "color" => "1e3673"] in PHP?

I'm writing an API. The API always returns JSON. Internally, the response data is an array that is converted to JSON like so:
<?php
function toJson($responseData) {
return json_encode($responseData, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
This makes sure that numbers are represented as integers (and not as strings) in the JSON. 99.9% of the time, this works great. However, it dies in the following instance:
<?php
toJson(array('color' => '1e3673'));
The reason it dies is that PHP thinks "1e3673" is a number in scientific notation and converts it into an integer. But the integer is too large, and ends up being represented as +INF, which cannot be represented in JSON. So json_encode fails completely.
How can I handle this situation correctly? It seems my options are
Somehow tell PHP that "1e3673" is not a number.
Stop using JSON_NUMERIC_CHECK. This will make the API really annoying to use with many languages (e.g. Javascript), and seems like a terrible way to handle this narrow corner case.
Write my own JSON encoder that avoids this issue.
I would prefer #1, but am open to #3 if that's necessary. Or maybe there's another way?
Turns out this is a bug, which is fixed in versions 5.5.23+, 5.6.7+, 7.0+.
https://3v4l.org/aHt34

What kind of object is this?

I'm trying to load this object in python or PHP, but I'm now trying to know if there are libraries that are already written so that I don't parse the document myself.
variable = [["1","arbitrary string","another arbitrary string"],
["2","arbitrary string","another arbitrary string"],
["3","arbitrary string","another arbitrary string"],
["4","arbitrary string","another arbitrary string"]];
another_variable = "arbitrary string";
Any hints will be appreciated.
Thanks
This looks a lot like JSON. PHP has some built-in functions to handle it - json_decode, for instance: http://www.php.net/manual/en/function.json-decode.php.
It looks like Python also has a JSON library built-in: http://docs.python.org/3/library/json.html (the same library is available in Python 2 as well, if you're still using the "older" version).
That is a JSON encoded string, representing an array of arrays.
You can make it a PHP native element using :
$var = json_decode($variable);
Do note that json_decode() is build-in to PHP after 5.2 only. Otherwise you'll have to get it from PEAR.

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

PHP mysql code help decoding

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!

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