Ray576
December 9, 2022, 2:55am
1
Hi all
Below is my json file example.
{
"Math_test": [{
"Student1": {
"Name": John,
"examination": {
"MCQ": 40,
"non-MCQ": 30,
"Score": 70
}
},
"Student2": {
"Name": Mary,
"examination": {
"MCQ": 30,
"non-MCQ": 30,
"Score": 60
}
},
"Student3": {
"Name": Peter,
"examination": {
"MCQ": 50,
"non-MCQ": 40,
"Score": 90
}
},
}],
"English_test:" [{
"Student1": {
"Name": John,
"examination": {
"MCQ": 28,
"non-MCQ": 32,
"Score": 60
}
},
"Student2": {
"Name": Mary,
"examination": {
"MCQ": 50,
"non-MCQ": 30,
"Score": 80
}
},
"Student3": {
"Name": Peter,
"examination": {
"MCQ": 35,
"non-MCQ": 20,
"Score": 55
}
},
}]
}
I want to get Peter's English test score in non-MCQ or Mary's Math test score in MCQ.
What luci api can do it and can you provide example code for reference ?
Thank you for your kind reply!
jow
December 9, 2022, 8:05am
2
Where is the JSON data stored, where should it be displayed?
Ray576
December 9, 2022, 8:17am
3
I use below to write json to specific file .
local f = io.open("/tmp/data", "a+")
f:write(json.stringify(json,true))
f:close()
I want it display on luci webgui which is a js file.
Thank you for your help. jow
jow
December 9, 2022, 8:25am
4
Ideally use fs.read()
Make sure to set an appropriate ACL (example: https://github.com/openwrt/luci/blob/master/modules/luci-mod-status/root/usr/share/rpcd/acl.d/luci-mod-status-index.json#L6 )
Example code:
'require fs';
// ...
fs.read('/tmp/data').then(function(result) {
let data = JSON.parse(result);
alert(data.Math_test.find(function(e) {
return e.Name == "Peter";
}).examination["non-MCQ"]);
});
Ray576
December 9, 2022, 9:33am
5
Hi jow,
May I ask below the part of example code you write , JSON mean luci.jsonc
this class api , is it right ?
let data = JSON.parse(result);
http://openwrt.github.io/luci/api/modules/luci.jsonc.html
Thank you !
jow
December 9, 2022, 10:40am
6
No, I mean the browser built in browser JSON API: