📘 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.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare 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