Newsletter

What Is JSON, Complete Introduction To JSON

Json » on Feb 1, 2012 { 5 Comments } By Sivateja

JSON is JavaScript Object Notation [ it’s the damn default definition πŸ™‚ ], and was invented by Douglas Crockford. Actually JSON is lightweight alternative to XML, language and platform independent script. Even JSON parsers & libraries are exists for many programming languages.

Converting JSON text to something in JavaScript will takes one line of code by using eval function πŸ˜‰ , to create an object from JSON text, just pass the string to eval function after surrounding the text with parentheses that’s it.

Creating JSON Object From JSON text

var names = "{"nameslist": ["java4s","php4s","visa4s"]}";
  • The variable names will hold the JSON text
  • Whole JSON text must be surrounded by quotes

Convert This Variable (names) Into JavaScript object

Actually we can convert JSON text into JavaScript object with eval function. eval() is the JavaScript function which converts JSON text into an JavaScript object, see this…

var MyJavaScriptObject = eval("(" + names + ")");

How To Access This JSON Object

var namesArray = MyJavaScriptObject["nameslist"];

Now you can print the values…

namesArray[0] will prints - java4s
namesArray[1] will prints - php4s
namesArray[2] will prints - visa4s

Note: Index always start from zero

Rather using eval() function It is safer to use a JSON parser to convert a JSON text to a JavaScript object. A JSON parser will recognize only JSON text and will not compile scripts, will see the complete example in the next tutorial.

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “What Is JSON, Complete Introduction To JSON”
  1. Hi, thank you for your tutorial.
    This are very useful to us. I want JUNIT tutorials with project example.
    Please share junit tutorial.
    Thank you for java4s

  2. Amit says:

    plz share junit tutorial.

  3. Hi,
    Can you please add MAVEN tutorials?

  4. RajaGopal says:

    HI siva your tutorial is very usefull to me thanks for giveing

    i want to expect SQL tutorial with queryes

  5. Narendra says:

    Hi, Java4s team.
    Thanks a lot for helping us in a simple and easy way

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.