3

I develop game using TypeScript. I have level.json file, which generated by level editor. How I can load this file in my game and read data from it?

1
  • What environment ? Node JS ? Commented Jun 19, 2014 at 8:32

1 Answer 1

5

Simplistically speaking, you could load it with an AJAX call and parse the JSON:

function levelRequestListener () {
    var levels = JSON.parse(this.responseText);
    console.log(levels);
}

var request = new XMLHttpRequest();
request.onload = levelRequestListener;
request.open("get", "level.json", true);
request.send();

You could take this up a level by writing an interface to describe the levels structure so you could get type checking and auto-completion on the levels variable...

interface Level {
    id: number;
    name: string;
}

function levelRequestListener () {
    var levels: Level[] = JSON.parse(this.responseText);
    console.log(levels[0].name);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.