I'm facing an issue with reading a json file in JavaScript. I've tried the code below, but it generated an error: Uncaught SyntaxError: Unexpected token ':'
<script>
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'weather.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(JSON.parse(xobj.responseText));
}
};
}
//usage:
loadJSON(function(json) {
console.log(json);
});
</script>
The json file I am trying to read contains this data:
{
"RegionPK{region='kirovohrad', observation=6, forecast=12, level=100000.0}":{
"temperatureStatistics":{
"count":26,
"sum":123.400146484375,
"min":3.850006103515625,
"max":6.75,
"average":4.746159480168269
},
"humidityStatistics":{
"count":26,
"sum":1797.7999954223633,
"min":56.599998474121094,
"max":79.4000015258789,
"average":69.1461536700909
},
"cloudnessStatistics":{
"count":17,
"sum":0.0,
"min":0.0,
"max":0.0,
"average":0.0
}
},
"RegionPK{region='dnipropetrovsk', observation=0, forecast=3, level=100000.0}":{
"temperatureStatistics":{
"count":46,
"sum":90.8670654296875,
"min":1.181884765625,
"max":2.481903076171875,
"average":1.975370987601902
},
"humidityStatistics":{
"count":46,
"sum":3598.199996948242,
"min":72.0,
"max":83.30000305175781,
"average":78.22173906409222
},
"cloudnessStatistics":{
"count":32,
"sum":0.0,
"min":0.0,
"max":0.0,
"average":0.0
}
}
}
The error is pointing at this section:
RegionPK{region='kirovohrad',observation=6,forecast=12,level=100000.0}":{
However, my file has passed json validation. Is there any way to read this file correctly or is my json file invalid?