Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing multiple JSON files in JavaScript

I have some sprite sheets with the atlus saved in a JSON format. I'm structuring my atlus based upon the structure from BrowserQuest. Each of their JSON files looks like:

{
    "id": "agent",
    "width": 24,
    "height": 24,
    "animations": {
        "idle_down": {
            "length": 2,
            "row": 0
        }
    },
    "offset_x": -4,
    "offset_y": -8
}

But I'm wondering, how do I even access the data in each JSON file, if it just a raw object literal?

Since each JSON file is an object literal, the only way I could imagine accessing it is to save the object literal to a variable, such as

var agent = {
    "id": "agent",
    "width": 24,
    "height": 24,
    "animations": {
        "idle_down": {
            "length": 2,
            "row": 0
        }
    },
    "offset_x": -4,
    "offset_y": -8
};

I'm hoping there is an easy way to access JSON files.

And because each individual sprite sheet has its own JSON file, I have a large number of files to load.

What is the best way to load such a high number of JSON files? I'm trying to avoid using any JS libraries.

like image 701
user22979 Avatar asked Aug 16 '13 01:08

user22979


People also ask

How do I combine multiple JSON files?

Step 1: Load the json files with the help of pandas dataframe. Step 2 : Concatenate the dataframes into one dataframe. Step 3: Convert the concatenated dataframe into CSV file.

Can JSON server watch multiple files?

It can only watch one file. You have to put the all info you need into the same file.

How do I open a JSON file in JavaScript?

Approach: Create a JSON file, add data in that JSON file. Using JavaScript fetch the created JSON file using the fetch() method. Display the data on the console or in the window.

Can JSON have multiple objects?

Can JSON have multiple objects? You can pass a single JSON object to create a single element, or a JSON array of group JSON objects to create multiple elements.


2 Answers

First, to answer your questions:

But I'm wondering, how do I even access the data in each JSON file, if it just a raw object literal?

JSON stands for JavaScript Object Notation, and as such is identical to how it would be manipulated if it was a JavaScript object.

As for accessing the JSON file if it was a local file, you could use this:

function doStuff(json){
    console.log(json);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function(){
    doStuff(JSON.parse(this.responseText));
});
oReq.open("GET", "http://www.example.com/example.json");
oReq.send();

And then you can just replace doStuff with whatever function handles the JSON.

like image 162
Quill Avatar answered Oct 05 '22 23:10

Quill


You can use XMLHttpObject.

Have a look

function getJSONFile(url,callback) {   
  var req = new XMLHttpRequest();
  req.open('GET', url, true); 
  req.overrideMimeType("application/json");
  req.onreadystatechange = function () {
      if (req.readyState == 4 && req.status == "200") {
        callback(req.responseText);
      }
  };
req.send();  
}

use this function like this

getJSONFile('http://www.example.com/example.json', function(data){
  if(data)
    console.log('json data : ' + JSON.stringify(data));
})
like image 45
Ravi Rupeliya Avatar answered Oct 06 '22 00:10

Ravi Rupeliya