How to parse JSON object in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Example

To parse JSON object in JavaScript, implement the following code −

<!DOCTYPE html>
<html>
   <body>
      <script>
         myData = JSON.parse('{"event1":{"title":"Employment period","start":"12\/29\/2011 10:20 ","end":"12\/15\/2013 00:00 "},"event2":{"title":"Employment period","start":"12\/14\/2011 10:20 ","end":"12\/18\/2013 00:00 "}}')
         myArray = []
         for(var e in myData){
            var dataCopy = myData[e]
            for(key in dataCopy){
               if(key == "start" || key == "end"){
                  dataCopy[key] = new Date(dataCopy[key])
               }
            }
            myArray.push(dataCopy)
         }
         document.write(JSON.stringify(myArray));
      </script>
   </body>
</html>
raja
Published on 29-Jan-2018 14:35:56
Advertisements