简体   繁体   中英

Extract data from .json file in typescript

I have an angular application. I can load the .json file into the application but I can't figure out the syntax for access the data in the file. I posted a similar question on this topic but I may have been too broad, I simply need to know how to access the data in a file I already have uploaded and that my application recognizes as application/json file

 startUpload(event: FileList){

    var file = event.item(0);

    console.log(file)    


  }

console output

File(139404) {name: "turnaroundtest.json", lastModified: 1527392751758, lastModifiedDate: Sat May 26 2018 20:45:51 GMT-0700 (PDT), webkitRelativePath: "", size: 139404, …}

try this.

console.log('name' + jsonObject.name); or

if (undefined !== jsonResponse.name && null !== jsonResponse.name){
    console.log('name' + jsonResponse.name);
}

if you have array of json then

for (const jsonObject in jsonResponse) {
    if (undefined !== jsonResponse[jsonObject].name && null !== jsonResponse[jsonObject].name) {
        console.log('name' + jsonResponse[jsonObject].name);
    }
}

Here is how you do it:

  detectFiles(event) {
    const files = event.target.files;
    if (event.target.files && event.target.files[0]) {
      const reader = new FileReader();
      reader.onload = ((e) => {
        const result = e.target['result'];
        console.log(JSON.parse(result));
      });
      reader.readAsText(event.target.files[0]);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM