[英]How to store and get data from a json file with javascript?
I would like to create a little kind of CMS. 我想创建一种CMS。 For that I want to code a function, which automatically creates a list of the files, which were created with the CMS. 为此,我想编写一个函数,该函数会自动创建使用CMS创建的文件列表。 This list sould be display in a html file and styled with CSS. 该列表将显示在html文件中,并使用CSS设置样式。 For that I want to create a json-File, in which i store the title and the location of the new file. 为此,我想创建一个json文件,在其中存储新文件的标题和位置。 Then it should look like this: 然后应该看起来像这样:
{
"new_sites":
{
"title": "source",
"otherTitle": "otherSource"
}
}
Now I want to know, how I can get (or store new) data from the json-File and use it as variables in javascript so that I can display it on the html page. 现在,我想知道如何从json文件获取(或存储新数据)并将其用作javascript中的变量,以便可以将其显示在html页面上。
Given these three variables: 鉴于以下三个变量:
var myNewSite = 'newSite';
var myNewTitle = 'newTitle';
var myNewSource = 'newSource';
and your final json variable initialized in this way: 然后以这种方式初始化您的最终json变量:
var myJson = {};
You could simply: 您可以简单地:
myJson[myNewSite] = {
myNewTitle: myNewSource
};
You can use Jquery api to get data from json-file.Suppose you have a data.json file in directory. 您可以使用Jquery api从json-file中获取数据。假设您在目录中有一个data.json文件。
<script>
var container={};
$.getJSON('data.json', function(data) {
container=data;
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.