How can I convert the PHP variable in server side to the JSON object?
I am thinking to first use JSON encode function and then after eval function. Is this the right way? Can I change directly or not?
why encode and decode process in server side? And what the serializer do in the client browser side?
So the flow of process: In server side encode php varialble to JSON string and the decode JSON string to Javascript Object and In server side perform serialization with stringify function and Parse to get data. Am I right?
Can not I directly send the JSON string after encoding?
Why do you want to use
eval()? After conversion to JSON, the object is represented as a string ready to transfer. You do not edit the JSON directly, but use your native objects until they are ready and then serialize them using JSON.On the server:
json_encode()(PHP docu)json_decode()(PHP docu)On the client (browser):
JSON.stringify()(MDN docu)JSON.parse()(MDN docu)EDIT
With respect to the question’s edit:
JSON is just another kind of serialization. Inspired by the short hand object/array notation of JavaScript. You can use it in almost any situation, where serialization is required (persisting objects, transferring objects over some network, …).
The big advantage of using JSON in web environments is its native support inside browsers and its rather compact form. Almost all browsers have some kind of JSON encode and decode functions and, if they do not, you can use libraries like jQuery to support JSON.