0

I need to create a little game simulator, but for that i need to consume a JSON file i have in my computer. The JSON is like a deck of cards and i need to put those cards into a player hand (array), but i'm looking everywhere and i don't get how i can consume the JSON file and insert it into my array.

I got here so far, but it does not work. Can someone aliviate my headache? I've been searching for ages, and i can only find people reliably people using Jquery, babel or typescript to consume it.

<script type="module">
    import * as data from './rounds.json';
       let arrayJoao = new Array(5);
       let arrayMaria = new Array(5);
       let roundsJogo = JSON.parse(data);
       console.log(roundsJogo[0]);
    </script>
  • JavaScript in the browser can't read files by itself, it can only read files that the user selects. – Barmar Mar 25 '20 at 00:20
  • If you are not using a server then you are probably loading files from the file:/// protocol, which is not valid for import syntax. If you have node installed a simple way to run a server is `npx serve `, the open http://localhost:5000 in your browser. Also, try importing a .js file instead of json and look at your developer console for errors. – Kevin Peña Mar 25 '20 at 01:35

0 Answers0