Skip to content
Advertisements

JavaScript JSON

Suppose we have a javaScript data and that we need to process this data on another page. Here, we need to move the data between pages in a specific format. Typically, plain text or “XML” was used for this. Because the “XML” format is difficult to interpret, a more easily interpreted data format was generated in javascript applications. The new format called “Javascript Object Notation” was developed.


Using JSON.stringify

JSON.stringify: Used to convert JavaScript to JSON format.

JSON.parse: Used to convert JSON objects into JavaScript.

var people = {
            name   : "Furkan",
            surname: "gülşen",
            age    : "21",
            information: ["html", "css", "javascript"],
            website: "www.codeblogger.wordpress.com"
        }
        var jsonpeople = JSON.stringify(people)
        alert(jsonpeople);

The JSON.stringify method takes an object and converts it to string.

The generated string is a JSON data ready to send. This data can be sent to another device for processing or to the database for saving.

Note that there is very little difference between the JSON data and the javaScript object. You must pay attention to these differences to create the correct json data.

JSON can also be applied to the following natural data types.

  • Object { … }
  • Array [ … ]
  • string,
  • number,
  • boolean :true/false,
  • null.
        // Number type
        alert(JSON.stringify(1)) // 1

        // String type data
        alert(JSON.stringify('test')) // "test"

        //boolean
        alert(JSON.stringify(true)); // true

        //array
        alert(JSON.stringify([1, 2, 3])); // [1,2,3]

The JSON data is of the cross-reference reference type. Therefore, some javaScript objects are not converted when converting with JSON.stringify.

  • Object Methods
  • Symbolic features
  • undefined

Using JSON.parse

The JSON.parse method is used to decode a data in JSON format and convert it to javascript format.

The converted structure from the JSON format can be data, an array, or an object. With parse transformation, the structure becomes available directly as a javascript object.

        var people = '{"name": "Furkan","surname": "gülşen","age":21,"information": ["html", "css", "javascript"],"website": "www.codeblogger.wordpress.com"}';

        var peoplaData = JSON.parse(people);
        alert(peoplaData.name);
        alert(peoplaData.surname);
        alert(peoplaData.age);
        alert(peoplaData.information);
        alert(peoplaData.website);
Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: