简体   繁体   中英

Print data from json file

I have json file with given data (total.json)

var data = {"trololo":{"info":"61511","path".... }}

I need to get object "info" and then print data "61511" in alert window I include my json like

var FILE = 'total'
var data_file_names = {};
data_file_names[FILE] = 'total.json';

And then i use it like

var data_trololo = data_file_names[FILE];

Plese, help me print object "info". Maybe there is another way to solve this problem

You need to make an ajax call to the json file. Then you can access the array like the below example.

Note : Your json wasn't properly formatted.

var data = {
    "trololo":{
        "info": ["61511","path"]
    }
};

console.log(data.trololo.info[0]); //this one will print 61511

Usually one can make an ajax call to read the file on the server.

But if you are ok with using HTML5 features then go through the link find out how to read the file on the browser itself. Though File API being part of HTML5 spec is stable across browsers.

http://www.html5rocks.com/en/tutorials/file/dndfiles/

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