There is a JSON file named devices.json
that contains information about various devices:
{
"Android":[
{"modell":"Samsung Galaxy S9+", "version":"8.0", "formfactor":"smartphone"},
{"modell":"Google Pixel 2XL", "version":"9.0", "formfactor":"smartphone"},
{"modell":"OnePlus One+", "version":"7.1.1", "formfactor":"smartphone"},
{"modell":"Motorola Moto G (3rd Generation)", "version":"6.0.1", "formfactor":"smartphone"},
{"modell":"Motorola Moto G (1st Generation)", "version":"5.1", "formfactor":"smartphone"},
{"modell":"Phicomm Clue C230", "version":"4.3", "formfactor":"smartphone"},
{"modell":"Huawei MediaPad M3 Lite 8", "version":"7.0", "formfactor":"tablet"},
{"modell":"Xiaomi Mi Pad", "version":"5.1", "formfactor":"tablet"},
{"modell":"Samsung Galaxy Tab 3 (7.0) Lite", "version":"4.2.2", "formfactor":"tablet"},
{"modell":"Amazon Fire Tablet 7 (5th Gen)", "version":"Fire OS 5.3.3", "formfactor":"tablet"}
],
"iOS":[
{"modell":"iPhone 7", "version":"12beta", "formfactor":"smartphone"},
{"modell":"iPhone 5s", "version":"11.4.1", "formfactor":"smartphone"},
{"modell":"iPad Air", "version":"11.4.1", "formfactor":"tablet"},
{"modell":"iPad 3", "version":"9.3.5", "formfactor":"tablet"}
],
"Windows":[
{"version":"Windows 10"},
{"version":"Windows 8.1"},
{"version":"Windows 7"}
],
"macOS":[
{"modell":"Macbook Air", "version":"10.13.5"}
],
"Linux":[
{"version":"Mint"},
{"version":"Kubuntu"},
{"version":"elemetaryOS"},
{"version":"openSUSE"},
{"version":"CentOS"},
{"version":"Fedora"},
{"version":"Oracle Linux"},
{"version":"Red Hat"}
]
}
I would like to parse this JSON data and display it in a particular format when a button is clicked:
Samsung Galaxy S9+ (Android 8.0), Google Pixel 2XL (Android 9), OnePlus One, etc...
However, I am unsure of the best way to parse the JSON data. Would something like this work?
<script>
function getArray(){
return $.getJSON('devices.json');
}
getArray().done( function(json) {
console.log(json); // show the json data in console
// Android
for(var i = 0; i < json.Android.length; i++) {
var obj = json.Android[i];
console.log(obj);
}
// iOS
for(var i = 0; i < json.iOS.length; i++) {
var obj = json.iOS[i];
console.log(obj);
}
});
</script>