I am generating a sequential string of independent JSON strings for insertion into a single field in my database (the means justify the end), and am wondering if a JSON string could ever legally have an occurrence of two opposing(ly) faced curly braces such as }{? As I would like to use this pattern as a delimiter if so.
I am using PHP's json_encode function for this purpose.
Should say that I don't mean as a value - or key if that were possible as I am in control of the data. Seems like a stupid question now.
Yes. It can form part of a string in JSON text.
{
"EskimoKiss": "}{"
}
If you must store multiple pieces of data expressed as JSON in a database field, then parse them to objects, wrap them in an array, then serialise that array to JSON and store that.
You really should normalise the data though.
Related
I am trying to take a string, which is actually part of a insert statement, explode it ( it is separated by commas ) and then test reform the string depending on the index of the value. I have everything done but realize that exploding by a comma while potentially having a serialized value with the whole string as its own value will actually explode that serialized value which throws the whole thing off. Im trying to avoid having to go back and check before creating the string, that would be very involved but might be my only option. I was wondering if there is a way to take the initial string and separate it by the commas without separating the actual serialized value within the string. An example is below.
NULL,NULL,NULL,NULL,'hello world','a:3:{s:6:"johnny";a:3:{s:7:"physics";s:9:"great job";s:5:"maths";s:19:"you did a, good job";s:9:"chemistry";s:27:"need to work on this, johny";}s:5:"brady";a:3:{s:7:"physics";s:9:"great job";s:5:"maths";s:19:"you did a, good job";s:9:"chemistry";s:27:"need to work on this, brady";}s:5:"keith";a:3:{s:7:"physics";s:9:"great job";s:5:"maths";s:19:"you did a, good job";s:9:"chemistry";s:27:"need to work on this, keith";}}',NULL,23,14
I am using json_encode to transform my php multidimensional array to output json. Normally, this function would convert all values to strings. To make sure that integers values are send to javascript as integer values, I am using the numeric check:
$json = json_encode($data, JSON_NUMERIC_CHECK);
This works fine in all but one case for my app. In the php array (which is extracted from the database), there is one field that contains very large integers. I save it to database as a VARCHAR, but unfortunately this is converted to an integer when encoding to json. The problem is that since this is a very large integer, it gets rounded and therefore does not represent the true value. How could I tackle this problem?
Do you want the large number to be transformed to an integer? Your question leads me to believe you don't. If that's the case, remove the JSON_NUMERIC_CHECK option from the call and it shouldn't change the encoding of the field.
Documentation about this (and other) constants is here.
Maybe is to late but i did hit the same problem, and stuck on PHP 5.3 on the server because of legacy code that must be run with that version. The solution that i used is dumb, but did work for me: simple add an space character at the end of the long integer that is varchar readed from the db, and before sending it to json encode with JSON_NUMERIC_CHECK.
I am writing some php code which which will be following the oauth2.0 specification. One of the requirements they specify is that any request in which parameters are repeated results in an error. As such, I have a json which I am parsing using json_decode, and I am trying to figure out how to catch if the json repeats any parameters. The result from json_decode seems to just use the last value for the key in the case of repeated parameters, so it seems like I would need to detect them before decoding. Does anyone know how to do this without writing my own json parser?
Thanks!
The approach that I ended up using was to json_encode the json decoded version of the input string and compare it to the input string. If the two match, there were no repeats. If they don't the json_decode saw the repeat, and removed it automatically, and hence there were repeats.
The methods of the dynamic Google Graph API want Javascript object literals as their argument, but I only have the data as JSON. What do I do? Is there any way that I could convert my JSON to an object literal?
The format they want is here:
http://code.google.com/intl/sv-SE/apis/chart/interactive/docs/reference.html#DataTable
I PHP, and also jQuery front end. I would appreciate front end or back end solutions. In the back end, I actually have the data in associative arrays so I can do anything with it there.
JSON is designed as a subset of Javascript object literal notation. Any* valid JSON code is already valid to interpret as a JavaScript literal.
* Technically, a few rare whitespace characters are treated differently, but this is relevant to almost nobody.
Is there a recommended way of sending an object using json between a server and a client ?
Should I use only lower case for properties, should I use an object or an array ?
Update: this question came to me because by default php is encoding an associative array as an object ( after you decode it)
You should make an array, then use PHP's json_encode method. It doesn't matter if the values are uppercase or lower case.
$a = array(
'Test' => 42,
'example' => 'Testing'
);
echo json_encode($a); // {"Test":42,"example":"Testing"}
When decoding in PHP, pass true as the 2nd parameter to json_decode to convert objects to arrays.
$data = json_decode($json, true);
Both these things are entirely up to you.
The casing of your property names is a coding style matter. It really doesn't matter as long as you are consistent -- your project should have fixed standards on this type of thing. If you haven't picked your standards yet, my advice is to go for readability, which usually means lower case or camel-case. I'd avoid upper case, and I'd also avoid using hyphens or underscores, but it is entirely up to you.
As for the choice between objects or arrays, that comes down to what is best suited to the data in question. If it needs named keys, then use an JSON object (ie with curly braces and key:value pairs {'key':'value','key2':'value2'}); if it doesn't, then use an JSON array (ie with square brackets and just values ['value1','value2']). The choice is entirely down to how the data needs to be structured: both are perfectly valid in JSON and neither is better than the other; just used for different purposes.
(PHP, of course, doesn't differentiate -- both keyed and indexed data are stored in PHP arrays, so from the PHP side it makes absolutely no difference).
Naming conventions aren't standardized, you can use whatever you like. It is a good idea to use names that are also valid javascript identifiers and won't clash with javascript keywords. Object vs. array is not a matter of convention, but rather one of meaning. A JSON object is a key-value collection, while an array is a flat list. Those are different things, and even though the syntax for both is somewhat interchangeable in javascript, and PHP can implement both using the same data type, you should make a clear distinction in your design. If it's a flat list, use []. If it's a key-value thing, use {}. On the PHP side, simply use arrays for both: numerically-indexed arrays for [], associative arrays for {}.