简体   繁体   中英

How to read data from JSON file with different key and value pairs

I have a set of data in a JSON file that consists of UserIDs (as keys) and Passwords (values). I want to read the same using JavaScript for validation purposes. My JSON is :

IDsNPass = '[{"A":"A15"},{"B":"B15"},{"C":"C15"}]';

I have an idea about how to access the data using JavaScript as long as the keys in the JSON remain the same. But here, the keys in my JSON are not constant all along. Any help on how to get along with the JavaScript is much appreciated!

First of all, your design is not a good format.

If you only have a id-password mapping in on object, you can do like this

var ip = JSON.parse(IDsNPass)

for(var obj in ip) {
  for(var key in obj) {
    console.info(key)
    console.info(obj[key])
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM