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.
Related
I have a whole suite of PHP scripts that interact with both the Android and iOS version of a mobile app. They all work the same: After the mobile app initiates a GET or POST, the PHP script typically returns a dash delimited string.
e.g.
If I want to get a list of the comments on a particular page, the PHP script would return something like
user1-comment1-user2-comment2
Is there a better way than this? Because if I ever want to return a new variable e.g.
user1-comment1-newValue1-user2-comment2-newValue2
then this will break all current versions of the mobile app.
Why not serialize the result and parse it in java? You could also use json_encode in PHP and decode it in your android app... see How to parse JSON in Android
You need to use a serializer. If you have a user name that contains a - you'll run into problems. Serializers take care of this for you. The current favorite is JSON, used to be XML.
JSON has excellent support in most web languages.
You can serialize your array of data into either a json string or a message pack string. Let's say your php array was:
$a = array(
"user1" => "comment1",
"user2" => "comment2",
"user3" => "comment3"
);
That would translate to this in json:
{
"user1": "comment1",
"user2": "comment2",
"user3": "comment3"
}
This json string can be easily converted to NSDictionaries on ios (using NSJSONSerialization) and JSONObjects on android (tutorial here).
A message packed string would be similar in structure to the json string above, but is less human readable because of its more compact nature. However, both Java and Objective-C have libraries to help translate messaged packed data into native objects.
Using JSON, you can make use of name/value pairs, so the order or inclusion of the parameters won't matter. JSON also provides hierarchy and limited typing, such as number versus string.
JSON also allows you to easily escape characters, so you could have any value you want (even with backslashes and quotes.
I have some data structure in PHP that I need to dump into a format that can be natively parsed as PHP code. Is there a tool for that?
In other words, is there a PHP data structure formatter that would output PHP object notation code, a sort of "PSON" for PHP similar to what JSON is for JavaScript?
Use build in function var_export.
There is, it is called serialize: http://php.net/manual/en/function.serialize.php
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);
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.