Thursday, August 31, 2017

String to JSON object

  •  A common use of JSON is to exchange data to/from a web server.
  • When receiving data from a web server, the data is always a string.
  • Parse the data with JSON.parse(), and the data becomes a JavaScript object.
Edit in plnkr

We would usually receive text from the server. We need to convert it into JavaScript object. In this scenario we use JSON.parse() function.

console.log("** Convert string into JSON object *****")
  
  var jsonstring='{"firstName":"Prathap","lastName":"Kudupu"}'
  
  console.log("\n string-->",jsonstring);
 
  var obj=JSON.parse(jsonstring);
  
  console.log("\nJSON-->",obj);



Output



No comments:

Post a Comment