📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Parse JSON String into JavaScript Object Prototype or Constructor Function
function Post(){
this.id = "";
this.title = "";
this.description = "";
this.postedUser = new User();
}
function User(){
this.id="";
this.name = "";
this.age = "";
}
var json2 = {
"post" : {
"id" : "1",
"title" : "post title",
"description" : "post description",
"postedUser" : {
"id" : "1",
"name" : "Ramesh",
"age" : "29"
}
}
}
function demo(){
// parse to json string
var jsonStr = JSON.stringify(json2);
// parse json string into JavaScript Object
var object = JSON.parse(jsonStr);
console.log(object);
console.log(object.getTitle());
}
demo();
Complete Code and Output
function Post(){
this.id = "";
this.title = "";
this.description = "";
this.postedUser = new User();
}
function User(){
this.id="";
this.name = "";
this.age = "";
}
var json2 = {
"post" : {
"id" : "1",
"title" : "post title",
"description" : "post description",
"postedUser" : {
"id" : "1",
"name" : "Ramesh",
"age" : "29"
}
}
}
function demo(){
// parse to json string
var jsonStr = JSON.stringify(json2);
// parse json string into JavaScript Object
var object = JSON.parse(jsonStr);
console.log(object);
console.log(object.getTitle());
}
demo();
Comments
Post a Comment
Leave Comment