I'm building a blogging site for learning, with a PHP/MySQl back-end. All of the user input is handled with forms sent in POST requests.
Will using JSON somehow make it cleaner, or easier to maintain or add features? Or am I just adding an interchange format for no reason?
So essentially, what functionality would be best implemented by using JSON?
5 Answers 5
JSON has a few advantages:
- It's a structured format, which can be validated and parsed with existing, mature tools.
- It can speak easily to JavaScript, which makes it very useful for AJAX communication.
- It's extremely simple and lightweight. Anything you'd want to use XML data interchange for, JSON is generally a better alternative.
My rule of thumb is, if you only need to return a single semantic element from a call, send it as plain text. But if you need to return multiple pieces of information, use JSON.
-
6in the speaking with JS part do not ever use eval to get the value of it instead use JSON.parse available in most browsersratchet freak– ratchet freak2012年12月18日 19:17:37 +00:00Commented Dec 18, 2012 at 19:17
-
1It speaks easily to PHP as well, since
json_decode()
andjson_encode()
convert between a JSON string and the nativeArray()
data structureIzkata– Izkata2012年12月18日 21:11:04 +00:00Commented Dec 18, 2012 at 21:11 -
@Izkata only if your server has those functions.Darth Egregious– Darth Egregious2012年12月18日 21:32:37 +00:00Commented Dec 18, 2012 at 21:32
-
Except in the case of form submission, using JSON breaks standards. Why fix what's not broken?Brendan Long– Brendan Long2012年12月18日 21:39:34 +00:00Commented Dec 18, 2012 at 21:39
-
2@BrendanLong: That standard doesn't say anything about using or not using JSON. Obviously if you're doing a form submission, you should encode all data according to the HTTP standard. But if one of the form elements you're sending is a complex item containing multiple semantic elements, putting that element in JSON format (which is then encoded correctly by your HTTP library) is a good way to do it.Mason Wheeler– Mason Wheeler2012年12月18日 21:43:18 +00:00Commented Dec 18, 2012 at 21:43
For what you describe - it sounds like a blogging platform where everything is submitted via forms - no, you dont need to convert it all to JSON. PHP handles forms seamlessly for you. There's no reason to introduce a new piece of complication in that situation.
Again, in your specific circumstance, JSON might be something you'd use if you needed to send semi-structured data back to the web brower. On the browser side, the JSON would be very easy to parse out in javascript.
JSON is only useful if you intend to make a JavaScript heavy site that uses Ajax requests to pass data to the server/get data to display without doing a full post-back. If you have no intention of making use of that functionality using JSON is just wrapping your code in another layer that has to be serialized/deserialized to do anything useful.
I don't think implementing JSON will inherently improve your site as is. JSON is JavaScript Object Notation; so unless you're starting to learn JavaScript as well, I don't see an inherent value in making sure everything is in JSON.
-
You don't need to learn JavaScript to use/abuse JSON. It's language-independent.CokoBWare– CokoBWare2012年12月18日 20:36:03 +00:00Commented Dec 18, 2012 at 20:36
While JSON is very extensible and well-structured, it's not the fastest one.
JSON is great for sending data from server to clients, because it resolves issues with encoding.
But on the server, you need maximum performance and minimum disc space consumption. So, for tables, you should use MySQL columns, and for non-tables a binary format is preferred.
To solve extensibility issues with binary files, you can tag your structures with a 4-character code for structure name and 1-2-byte number for version.
-
6JSON is a data format, not a database.Robert Harvey– Robert Harvey2012年12月18日 19:16:33 +00:00Commented Dec 18, 2012 at 19:16
-
Yes, but it can be used to store arrays.Triang3l– Triang3l2012年12月18日 19:25:15 +00:00Commented Dec 18, 2012 at 19:25
I'm building a blogging site for learning
- your learning the associated technologies? or to provide material for others? If the former, absolutely - use json for the sake of using it so you learn to use it. Challenge yourself with new technologies - otherwise, you won't learn how to use them.