Html5文件读取器-读取本地Json数组文件并仅显示特定部分

Html5 filereader - read local Json Array file and display only a specific section

本文关键字:文件 读取 显示 定部 Json Html5 数组      更新时间:2023-09-26

我是一个初学者,来自VBA excel编程工具。在VBA中读取excel文件、操作excel内容比使用Filereader和Json数组等web工具要容易得多。

我在Json数组文件中有以下内容。

[
  ["TWE",6000,4545.5  ],
  ["RW",1000,256.3  ]
]

我想从下面的html文件中读取,并只显示值253.6

你能帮我吗?

这里的Html文件读取器示例

<!DOCTYPE html>
<html>
    <head>
        <script>        
            function handleFileSelect()
            {               
                if (window.File && window.FileReader && window.FileList && window.Blob) {
                } else {
                    alert('The File APIs are not fully supported in this browser.');
                    return;
                }   
                input = document.getElementById('fileinput');
                if (!input) {
                  alert("Um, 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() {           
               //result = fr.result;
               document.getElementById('editor').appendChild(document.createTextNode(fr.result))
            }           
        </script>
    </head>
    <body>
        <input type="file" id="fileinput"/>
        <input type='button' id='btnLoad' value='Load' onclick='handleFileSelect();'>
        <div id="editor"></div>
    </body>
</html>

如果将fr.readAsText中的文本转换为字符串,则可以使用JSON.parse()(请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)将字符串转换为JSON对象。然后,您可以像访问普通数组一样访问您的特定内容。