-4

I am trying to iterate this type json format, i tried to follow this method bit confused with my code How to iterate JSON array in JavaScript?.

and also i have an doubt, is this realy a json format or some thing else because i never seen such formats

 {  
   "pageId":"2001",
   "segments":"15",
   "cacheable":"Y",
   "compagedetailsails":[  
      {  
         "compid":"MOTP_HELP_TXT",
         "masterkey":"104_MOTP_HELP_TXT",
         "pagedetails":[  
            {  
               "key":"MOTP_HELP_TXT",
               "val":[  
                  "One Time Password has been sent to your mobile number. Click on resend in XXX seconds."
               ]
            }
         ]
      },
      {  
         "compid":"MOTP_EXP_SEC",
         "masterkey":"104_MOTP_EXP_SEC",
         "pagedetails":[  
            {  
               "key":"MOTP_EXP_SEC",
               "val":[  
                  "120"
               ]
            }
         ]
      },
      {  
         "compid":"MOTP_SUCC_ACT",
         "masterkey":"104_MOTP_SUCC_ACT",
         "pagedetails":[  
            {  
               "key":"MOTP_SUCC_ACT",
               "val":[  
                  "LMONUSPR"
               ]
            }
         ]
      },
      {  
         "compid":"bcksignotp",
         "masterkey":"104_bcksignotp",
         "pagedetails":[  
            {  
               "key":"N104_bcksignotp0",
               "val":[  
                  "mxback"
               ]
            },
            {  
               "key":"N104_bcksignotp1",
               "val":[  
                  ""
               ]
            },
            {  
               "key":"N104_bcksignotp2",
               "val":[  
                  "0"
               ]
            }
         ]
      }



   ]
}
Community
  • 1
  • 1
Divya Sharma
  • 237
  • 1
  • 6
  • 17
  • 2
    what exactly are you struggling with? Post the code you have written so far. And yes, the data you've given is JSON. I haven't checked whether it's valid, but it certainly looks like the right format. – ADyson Jun 20 '16 at 13:06
  • I am trying to render each value by using masterkey – Divya Sharma Jun 20 '16 at 13:23
  • you still need to post your code. That isn't a sufficient description for anyone to see where the problem might be. Equally, you're unlikely to get any help if you don't show that you have made some effort to solve the problem yourself. – ADyson Jun 20 '16 at 13:25
  • Please specify the context(code) in order for anyone to answer your question. – Hassan Abbas Jun 20 '16 at 13:28

1 Answers1

0

a = {  
   "pageId":"2001",
   "segments":"15",
   "cacheable":"Y",
   "compagedetailsails":[  
      {  
         "compid":"MOTP_HELP_TXT",
         "masterkey":"104_MOTP_HELP_TXT",
         "pagedetails":[  
            {  
               "key":"MOTP_HELP_TXT",
               "val":[  
                  "One Time Password has been sent to your mobile number. Click on resend in XXX seconds."
               ]
            }
         ]
      },
      {  
         "compid":"MOTP_EXP_SEC",
         "masterkey":"104_MOTP_EXP_SEC",
         "pagedetails":[  
            {  
               "key":"MOTP_EXP_SEC",
               "val":[  
                  "120"
               ]
            }
         ]
      },
      {  
         "compid":"MOTP_SUCC_ACT",
         "masterkey":"104_MOTP_SUCC_ACT",
         "pagedetails":[  
            {  
               "key":"MOTP_SUCC_ACT",
               "val":[  
                  "LMONUSPR"
               ]
            }
         ]
      },
      {  
         "compid":"bcksignotp",
         "masterkey":"104_bcksignotp",
         "pagedetails":[  
            {  
               "key":"N104_bcksignotp0",
               "val":[  
                  "mxback"
               ]
            },
            {  
               "key":"N104_bcksignotp1",
               "val":[  
                  ""
               ]
            },
            {  
               "key":"N104_bcksignotp2",
               "val":[  
                  "0"
               ]
            }
         ]
      }



   ]
}
for(var i = 0; i < a.compagedetailsails.length; i++)
{
  console.log(a.compagedetailsails[i]);
}

You can iterate something like this and through this you can access each element and its attributes. I hope it helps ! Furthermore you can access anything in the Json using dot operator.

Hassan Abbas
  • 1,166
  • 20
  • 47