以下相应的匹配被JSON文件比较两个JSON文件ID的值作为比较因子和显示使用JavaScript
1.question.json
[{
"Question":"Name any 5 flowers",
"id":["158_-121634659","158_-195702286","158_86710893","158_18978332","158_-58432062"]
}]
2.answer.json
[{
"Group": "Beautiful",
"id": "158_-121634659",
"SubField": "Cloud Pricing and ROI Calculators",
"Module": "Flowers",
}, {
"Group": "colourful",
"Field": "Related Links",
"id": "158_-195702286",
"SubField": "Guidance Toold to Determine which Subscription to include in Solution",
"Module": "Flowers",
}, {
"Group": "attractive",
"Field": "Related Links",
"id": "158_86710893",
"SubField": "Customizations & Integrations",
"Module": "Flowers",
}, {
"Group": "Sweetsmelling",
"Field": "Related Links",
"id": "158_18978332",
"SubField": "Market Price Guidance",
"Module": "Flowers",
}, {
"Group": "Large",
"Field": "Cost Guidance",
"id": "158_-58432062",
"SubField": "",
"Module": "Flowers",
}]
我需要将ID数组的每个元素与另一个文件中的每个json对象(id)进行比较,并显示与ID相关的相应内容(如果匹配)。
function jsonLoadFile() {
var input, file, fr;
if (typeof window.FileReader !== 'function') {
alert("The file API is not supported in this browser");
return;
}
input = document.getElementById('fileinput');
if (!input) {
alert("couldn't find the fileinput element");
}
else if (!input.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!input.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
file = input.files[0];
fr = new FileReader();
fr.onload = receivedText;
fr.readAsText(file);
}
function receivedText(e) {
lines = e.target.result;
var newArr = JSON.parse(lines);
for(i=0; i<newArr.length;i++){
var span = document.createElement("span");
var data= "<b>"+newArr[i].Question+"</b>";
document.getElementById('myContent').innerHTML= myContent.innerHTML+ data;
}
}
}
function jsonLoad() {
var output,files, f;
if (typeof window.FileReader !== 'function') {
alert("The file API is not supported in this browser");
return;
}
output = document.getElementById('fileoutput');
if (!output) {
alert("couldn't find the fileinput element");
}
else if (!output.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!output.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
files = output.files[0];
f = new FileReader();
f.onload = receivetext;
f.readAsText(files);
}
function receivetext(e){
content=e.target.result;
var Arr=JSON.parse(content);
for(j=0;j<Arr.length;j++){
var s=document.createElement("s");
s.setAttribute("class", "");
var sp=s.outerHTML;
if(Arr[j].SubField != ""){
var d= sp+' '+"<b>"+Arr[j].SubField+"("+
Arr[j].Group+")"+"</b>"+"<br>"+"<br>"+' '+"ID : "+Arr[j].id+"<br>"+
Arr[j].Content+"<br>";
}
else {
var d= sp+' '+"<b>"+Arr[j].Field+"("+
Arr[j].Group+")"+"</b>"+"<br>"+"<br>"+' '+"ID : "+Arr[j].id+"<br>"+
Arr[j].Content+"<br>";
}
document.getElementById('Contents').innerHTML= Contents.innerHTML+ d;
}
}在上面的代码中,我使用的两个的FileReader()函数两个 }
读取两个不同的JSON文件根据条件来显示的JSON内容
在哪里可以实施比较代码
我看不出有任何的代码比较ID的......是你的问题,如何编写代码?您可能需要2个嵌套循环遍历每个ID与其他文件中的每个ID,然后移动到第一个文件中的第二个ID等等。这是否有帮助? – Shazam
如何在im同时加载两个json文件时编写代码,比较json对象并显示 – HK123
您是否试过嵌套使用文件读取器?或创建两个不同的文件读取器对象? – Shazam