The JavaScript JSON is an acronym of JavaScript Object Notation. It provides a format for storing and transporting data. It is a lightweight human readable collection of data that can be accessed in a logical manner. The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.
Points to remember It generates and stores the data from user input. It can transport the data from the server to client, client to server, and server to server. It can also build and verifying the data. JavaScript JSON Example <script> //JavaScript to illustrate JSON.parse() method. var j = '{"Name":"Krishna","Email": "XYZ", "CN": "12345"}'; var data = JSON.parse(j); document.write("Convert string in JSON format using parse() method<br>"); document.write(data.Email); //expected output: XYZ //JavaScript to illustrate JSON.stringify() method. var j = {Name:"Krishna", Email: "XYZ", CN : 12345}; var data = JSON.stringify(j); document.write("<br>Convert string in JSON format using stringify() method<br>"); document.write(data); //expected output: {"Name":"Krishna","Email":"XYZ","CN":12345} </script> JSON Objects JSON objects are written inside curly braces. Just like in JavaScript, objects can contain multiple name/value pairs: {"firstName":"John", "lastName":"Doe"} JSON Arrays JSON arrays are written inside square brackets. Just like in JavaScript, an array can contain objects: "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ] In the example above, the object "employees" is an array. It contains three objects. Why Use JSON? The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a JavaScript program can easily convert JSON data into JavaScript objects. Since the format is text only, JSON data read json file using php can easily be sent between computers, and used by any programming language. JavaScript has a built in function for converting JSON strings into JavaScript objects: JSON.parse() The JSON.parse() does the reverse opposite of the JSON.stringify() does. It simply converts the JSON string into an Object. JavaScript also has a built in function for converting an object into a JSON string: JSON.stringify() The JSON.stringify() static method is used to convert the JSON object into a string. This is used in a JavaScript environment. However, keep in mind that a valid JSON object syntax is a valid object in JavaScript. To see how this works, copy the JSON code below and paste it into a JavaScript file and then store it in a variable called person. A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and put methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object. A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get and opt methods, or to convert values into a JSON text using the put and toString methods. A get method returns a value if one can be found, and throws an exception if one cannot be found. An opt method returns a default value instead of throwing an exception, and so is useful for obtaining optional values. The generic get() and opt() methods return an object, which you can cast or query for type. There are also typed get and opt methods that do type checking and type coercion for you. The opt methods differ from the get methods in that they do not throw. Instead, they return a specified value, such as null.
0 Comments
Leave a Reply. |
Boost your w3 visibility. Drive Sales using the power of Article Marketing. Rank higher with our lifelong SEO friendly Do-Follow links.
hot Bengali BoudisOur ArchivesOur BlogsImportance of Master Dynamic Backlink
Health benefits of Banana On speaking and English language education Writing Skills in English Center of Excellence Roles and Responsibility According to Numerology Signs of Diabetes Keto Diet Review Easy ways to keep your Skin Hydrated this Summer Vitamin K Is the Ingredients Said to Fade Dark Circles – Does it Work? Cashew is Healthy for individuals with Diabetes, Hypertension What is Moringa? Rules behind use of Articles in English Grammar with Examples Natural Hair Care Tips for Housewives Lead a Healthy Life Forever with Our Horoscope Health Problem Solutions Lingerie for Women |