Newsletter |
What Is JSON, Complete Introduction To JSON
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 .:: | ||
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
plz share junit tutorial.
Hi,
Can you please add MAVEN tutorials?
HI siva your tutorial is very usefull to me thanks for giveing
i want to expect SQL tutorial with queryes
Hi, Java4s team.
Thanks a lot for helping us in a simple and easy way