2017-01-10 105 views
1

以下相应的匹配被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内容

在哪里可以实施比较代码

+0

我看不出有任何的代码比较ID的......是你的问题,如何编写代码?您可能需要2个嵌套循环遍历每个ID与其他文件中的每个ID,然后移动到第一个文件中的第二个ID等等。这是否有帮助? – Shazam

+0

如何在im同时加载两个json文件时编写代码,比较json对象并显示 – HK123

+0

您是否试过嵌套使用文件读取器?或创建两个不同的文件读取器对象? – Shazam

回答

1

的语法可以是这样的:

var question = [{ 
"Question":"Name any 5 flowers", 
"id":["158_-121634659","158_-195702286","158_86710893","158_18978332","158_-58432062"] 
}]; 

var answer = [{ 
    "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", 

}]; 

for(i = 0;i < answer.length; i++){ 
    for(j = 0;j < question.length; j++){ 
     if(question[j].id.indexOf(answer[i].id) > 0){ 
     console.log("Match"); 
    } 
} 

indexOf会给你身份证的存在与否。

以下部分做的比较:

for(i = 0;i < answer.length; i++){ 
    for(j = 0;j < question.length; j++){ 
     if(question[j].id.indexOf(answer[i].id) > 0){ 
     console.log("Match"); 
    } 
} 
+0

谢谢.....它帮助..:)@ pritam,如果我有很多问题有一些类似的ID与他们的答案和answer.json文件与许多ID的,如果我有两个不同的JSON文件之一的问题和其他答案存储在本地变量[链接](https://plnkr.co/edit/tfWu4it7ryUKzbyqmof7?p=preview)......我怎么能比较每个问题ID的@时间与JSON文件和显示按照我的html结构 – HK123

+0

@ HK123对不起。我没有得到你的问题。你能否详细说明一下? –

+0

根据您的代码,如果我们有单个问题,我们可以存储在一个变量中,并可以与其他答案文件进行比较.......假设如果我有多个不同Id的问题,但是Id在其他json文件中匹配....比较逻辑如何工作以显示多个问题的答案 – HK123