• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 110
  • Last Modified:

JSON string to object

Looking at below example


<!DOCTYPE html>
<html>
<body>

<h2>Create Object from JSON String</h2>

<p id="demo"></p>

<script>

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;

</script>

</body>
</html>

Open in new window


when they say Object from JSON string which is JSON string which is output object(is it is javascript object or json object or java object or something else)??

please advise
0
gudii9
Asked:
gudii9
3 Solutions
 
ste5anSenior DeveloperCommented:
Just a small hint: JSON.parse()
0
 
aikimarkCommented:
more hints
* In the following statement
<p id="demo"></p>

Open in new window

"demo" is a string literal

* objects have properties and methods
obj.name refers to the name property of the obj variable
0
 
Julian HansenCommented:
JSON - JavaScript Object Notation
A way of expressing a JavaScript object as a string so it can be exchanged with other systems in a safe way.

This is a JavaScript object
var myObject {
   name: "Bruce Wayne",
  email: "batman@batcave.com"
};

Open in new window


It can be used like this
console.(myObject.name); // Outputs Bruce Wayne

Open in new window

Now, if we want to send this object say to a server service we can't send the object as this a memory structure that does not translate into sending over a text based medium like an HTTP link. So we need a way to express this object in a way that it can be sent using a text medium. This is where JSON comes in.

If we look at the JSON String for the above we get
'{"name":"Bruce Wayne", "email" : "batman@batcave.com"}'

Open in new window

We can also express it like this (if we want to make it more readable)
{
  "name":"Bruce Wayne", 
  "email" : "batman@batcave.com"
}

Open in new window

Notice that the structure is very similar to our JavaScript object - in fact the only difference is that the keys are enclosed in double quotes.

JavaScript supports keys being enclosed in double quotes so we could take that JSON string and do this
var newObject = {
  "name":"Bruce Wayne", 
  "email" : "batman@batcave.com"
}

Open in new window

And it would be perfectly legal

However, in most instances we are receiving (or sending) JSON from (to) a service - we can't simply assign the string to a variable and expect JavaScript to know it is an object - likewise we can't assign an object to a paramter in a URL an expect JavaScript to express it as a string.

That is where JSON.parse() an JSON.stringify() come in - they are functions that allow us to change a string (JSON) into a JavaScript memory object and vice versa.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
gudii9Author Commented:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

so they are converting json to javascript object
and then they are printign as below??
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
That is where JSON.parse() an JSON.stringify() come in - they are functions that allow us to change a string (JSON) into a JavaScript memory object and vice versa.
not clear.
please elaborate
0
 
Julian HansenCommented:
I can't make it any clearer.
JSON.parse() takes a string - usually from something like an AJAX call and turns it into a memory object that JavaScript understands
JSON.stringify() is the reverse - it takes a JavaScript object and turns it into a string version.

Which part is not clear?
0
 
gudii9Author Commented:
where can i check json API to see all input type of a method and its return type
please advise
0
 
gudii9Author Commented:
where can i check json API to see all input type of a method and its return type
please advise
0
 
Julian HansenCommented:
You can find the documentation for the JSON object here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now