Consider the following:
{"name":"John", "age":31, "city":"New York"}
Above example is a JSON object. The JSON format is almost identical to JavaScript objects.
Difference between JSON and JS object
Difference 1:
In JSON, keys must be strings, written with double quotes:
{ "name":"John" }
In JavaScript, keys can be strings, numbers, or identifier names:
{ name:"John" }
Difference 2:
In JSON, string values must be written with double quotes:
{ "name":"John" }
In JavaScript, you can write string values with double or single quotes:
{ name:'John' }
Functions
We send/receive data from server to client using only string format. Object format is not supported by http.
JSON.parse(): Text(string) to Javascript object/array. Consider, it removes single quotes from both sides of the string.
JSON.stringify():JavaScript function/array to string. Consider, it adds single quote to both sides of the string.
json_encode(): php object to string.
json_decode(): php string to object.
Laravel and REST API
->get(): Returns in a format [{}, {}…]
->fin(): Returns in a format {…}
Labels: JavaScript, Web development