0

I am stuck on how to parse this JSON using Javascript:

var JSONobj="{ 
  "sec" : {
    "username"  : "ac12",
    "location"  : "Indian Ocean",
    "online"    : true
  },
  "sec" : {
    "username"  : "gg66",
    "location"  : "Pacific Ocean",
    "online"    : false
  },
  "sec" : {
    "username"  : "test123",
    "location"  : "Atlantic Ocean",
    "online"    : false
  }
}";

var obj = JSON.parse(JSONobj);

I would then like to loop through each "sec" in turn, and extract the username into var UN, eg:

Sec - username = ac12
Sec - username = gg66

etc...

I just can't figure out how to do it.

I would like to use native JavaScript if possible, and not any libraries like jQuery.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sylv99
  • 151
  • 8
  • 3
    in reality you have only one `sec` or you get an error in strict mode. – Nina Scholz Mar 16 '18 at 07:37
  • 1
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) What you have in the question is invalid, so we have to guess at what you're *really* dealing with. That means **we cannot help you with the question as written**. Please update the question with code with valid syntax (no unescaped line breaks in strings, escaped double quotes in a double-quote quoted string, etc.). – T.J. Crowder Mar 16 '18 at 07:39

0 Answers0