JSON 来自网站 "pure" JavaScript > 提取并放入变量
JSON from website with "pure" JavaScript > exract and put into variables
我想为 iOS 创建一个新的可编写脚本的小部件,显示所谓的“血型晴雨表”,表示德国红十字会血液储备的当前状态。
我找到了this website,其中status在源代码中给出,形式为JSON:
<script type="application/json" data-drupal-selector="drupal-settings-json">
{"path":{"baseUrl":"\/","scriptPath":null,"pathPrefix":"","currentPath":"node\/3","currentPathIsAdmin":false,"isFront":false,"currentLanguage":"de"},"pluralDelimiter":"\u0003","blutgruppen":{"default":{"blood_barometer_a_plus":"1","blood_barometer_b_plus":"2","blood_barometer_ab_plus":"4","blood_barometer_zero_plus":"2","blood_barometer_a_neg":"1","blood_barometer_b_neg":"1","blood_barometer_ab_neg":"2","blood_barometer_zero_neg":"1"},"blood_barometer_changed":"2022-04-22"},"user":{"uid":0,"permissionsHash":"09f524fbefd35c1e3e7cc2b74fe2992115d7821527911825e868534003f88b7a"}}
</script>
格式化为可读的JSON格式:
{
"path":{
"baseUrl":"\/",
"scriptPath":null,
"pathPrefix":"",
"currentPath":"node\/3",
"currentPathIsAdmin":false,
"isFront":false,
"currentLanguage":"de"
},
"pluralDelimiter":"\u0003",
"blutgruppen":{
"default":{
"blood_barometer_a_plus":"1",
"blood_barometer_b_plus":"2",
"blood_barometer_ab_plus":"4",
"blood_barometer_zero_plus":"2",
"blood_barometer_a_neg":"1",
"blood_barometer_b_neg":"1",
"blood_barometer_ab_neg":"2",
"blood_barometer_zero_neg":"1"
},
"blood_barometer_changed":"2022-04-22"
},
"user":{
"uid":0,
"permissionsHash":"09f524fbefd35c1e3e7cc2b74fe2992115d7821527911825e868534003f88b7a"
}
}
据此,我想将以下值读入 JS 变量:
"blutgruppen":{
"default":{
"blood_barometer_a_plus":"1",
"blood_barometer_b_plus":"2",
"blood_barometer_ab_plus":"4",
"blood_barometer_zero_plus":"2",
"blood_barometer_a_neg":"1",
"blood_barometer_b_neg":"1",
"blood_barometer_ab_neg":"2",
"blood_barometer_zero_neg":"1"
},
"blood_barometer_changed":"2022-04-22"
}
关键是,Scriptable 不能与 jQuery 一起使用,所以我不能使用以下脚本,该脚本链接在上述网站(摘录):
jQuery('.blutbeutel-wrapper').each(function () {
let bestand = drupalSettings.blutgruppen.default[jQuery(this).data('id')];
var prozent = 11 + (12 * bestand);
jQuery(this).find('.blut').css({'height': prozent + '%'});
animationDone = true;
});
任何提示,我怎么能
a) 从网站的源代码中用“纯 JS”读取 JSON 和
b) 将其值提取到变量中?
更新 1
const url = `https://www.blutspende-leben.de/blut-spenden`;
let request = new Request(url)
let payload = await request.loadString()
let stack = widget.addStack()
const infoText = stack.addText(payload);
完整字符串的相关文本部分:
<script type=\"application/json\" data-drupal-selector=\"drupal-settings-json\">{\"path\":{\"baseUrl\":\"\/\",\"scriptPath\":null,\"pathPrefix\":\"\",\"currentPath\":\"node\/3\",\"currentPathIsAdmin\":false,\"isFront\":false,\"currentLanguage\":\"de\"},\"pluralDelimiter\":\"\u0003\",\"blutgruppen\":{\"default\":{\"blood_barometer_a_plus\":\"1\",\"blood_barometer_b_plus\":\"2\",\"blood_barometer_ab_plus\":\"4\",\"blood_barometer_zero_plus\":\"2\",\"blood_barometer_a_neg\":\"1\",\"blood_barometer_b_neg\":\"1\",\"blood_barometer_ab_neg\":\"2\",\"blood_barometer_zero_neg\":\"1\"},\"blood_barometer_changed\":\"2022-04-22\"},\"user\":{\"uid\":0,\"permissionsHash\":\"09f524fbefd35c1e3e7cc2b74fe2992115d7821527911825e868534003f88b7a\"}}</script>\n<script src=\"/sites/default/files/js/js_XYyrLSwI4XKIZtjKBiSeUOL1F_2b9VOK6r4ZEqc1CSQ.js\"></script>
let obj=JSON.parse(document.querySelector('script[type="application/json"]').innerHTML);
let result={};
result.blutgruppen=obj.blutgruppen;
result.blood_barometer_changed=obj.blood_barometer_changed;
console.log(result)
我想为 iOS 创建一个新的可编写脚本的小部件,显示所谓的“血型晴雨表”,表示德国红十字会血液储备的当前状态。
我找到了this website,其中status在源代码中给出,形式为JSON:
<script type="application/json" data-drupal-selector="drupal-settings-json">
{"path":{"baseUrl":"\/","scriptPath":null,"pathPrefix":"","currentPath":"node\/3","currentPathIsAdmin":false,"isFront":false,"currentLanguage":"de"},"pluralDelimiter":"\u0003","blutgruppen":{"default":{"blood_barometer_a_plus":"1","blood_barometer_b_plus":"2","blood_barometer_ab_plus":"4","blood_barometer_zero_plus":"2","blood_barometer_a_neg":"1","blood_barometer_b_neg":"1","blood_barometer_ab_neg":"2","blood_barometer_zero_neg":"1"},"blood_barometer_changed":"2022-04-22"},"user":{"uid":0,"permissionsHash":"09f524fbefd35c1e3e7cc2b74fe2992115d7821527911825e868534003f88b7a"}}
</script>
格式化为可读的JSON格式:
{
"path":{
"baseUrl":"\/",
"scriptPath":null,
"pathPrefix":"",
"currentPath":"node\/3",
"currentPathIsAdmin":false,
"isFront":false,
"currentLanguage":"de"
},
"pluralDelimiter":"\u0003",
"blutgruppen":{
"default":{
"blood_barometer_a_plus":"1",
"blood_barometer_b_plus":"2",
"blood_barometer_ab_plus":"4",
"blood_barometer_zero_plus":"2",
"blood_barometer_a_neg":"1",
"blood_barometer_b_neg":"1",
"blood_barometer_ab_neg":"2",
"blood_barometer_zero_neg":"1"
},
"blood_barometer_changed":"2022-04-22"
},
"user":{
"uid":0,
"permissionsHash":"09f524fbefd35c1e3e7cc2b74fe2992115d7821527911825e868534003f88b7a"
}
}
据此,我想将以下值读入 JS 变量:
"blutgruppen":{
"default":{
"blood_barometer_a_plus":"1",
"blood_barometer_b_plus":"2",
"blood_barometer_ab_plus":"4",
"blood_barometer_zero_plus":"2",
"blood_barometer_a_neg":"1",
"blood_barometer_b_neg":"1",
"blood_barometer_ab_neg":"2",
"blood_barometer_zero_neg":"1"
},
"blood_barometer_changed":"2022-04-22"
}
关键是,Scriptable 不能与 jQuery 一起使用,所以我不能使用以下脚本,该脚本链接在上述网站(摘录):
jQuery('.blutbeutel-wrapper').each(function () {
let bestand = drupalSettings.blutgruppen.default[jQuery(this).data('id')];
var prozent = 11 + (12 * bestand);
jQuery(this).find('.blut').css({'height': prozent + '%'});
animationDone = true;
});
任何提示,我怎么能 a) 从网站的源代码中用“纯 JS”读取 JSON 和 b) 将其值提取到变量中?
更新 1
const url = `https://www.blutspende-leben.de/blut-spenden`;
let request = new Request(url)
let payload = await request.loadString()
let stack = widget.addStack()
const infoText = stack.addText(payload);
完整字符串的相关文本部分:
<script type=\"application/json\" data-drupal-selector=\"drupal-settings-json\">{\"path\":{\"baseUrl\":\"\/\",\"scriptPath\":null,\"pathPrefix\":\"\",\"currentPath\":\"node\/3\",\"currentPathIsAdmin\":false,\"isFront\":false,\"currentLanguage\":\"de\"},\"pluralDelimiter\":\"\u0003\",\"blutgruppen\":{\"default\":{\"blood_barometer_a_plus\":\"1\",\"blood_barometer_b_plus\":\"2\",\"blood_barometer_ab_plus\":\"4\",\"blood_barometer_zero_plus\":\"2\",\"blood_barometer_a_neg\":\"1\",\"blood_barometer_b_neg\":\"1\",\"blood_barometer_ab_neg\":\"2\",\"blood_barometer_zero_neg\":\"1\"},\"blood_barometer_changed\":\"2022-04-22\"},\"user\":{\"uid\":0,\"permissionsHash\":\"09f524fbefd35c1e3e7cc2b74fe2992115d7821527911825e868534003f88b7a\"}}</script>\n<script src=\"/sites/default/files/js/js_XYyrLSwI4XKIZtjKBiSeUOL1F_2b9VOK6r4ZEqc1CSQ.js\"></script>
let obj=JSON.parse(document.querySelector('script[type="application/json"]').innerHTML);
let result={};
result.blutgruppen=obj.blutgruppen;
result.blood_barometer_changed=obj.blood_barometer_changed;
console.log(result)