Reputation: 67
Hello i'm trying to read this list from a file
this
this.testJson = {
list:[
{src:"x1.jpg",title:"x1",song:"x1.mp3"},
{src:"x2.jpg",title:"x2",song:"x2.mp3"},
{src:"x3.jpg",title:"x3",song:"x3.mp3"},
{src:"x4.jpg",title:"x4",song:"x4.mp3"}
]
}
to
this.testJson = {
list:[
// read x.txt or x.txt from a URL
]
}
and x.txt contain
{src:"x1.jpg",title:"x1",song:"x1.mp3"},
{src:"x2.jpg",title:"x2",song:"x2.mp3"},
{src:"x3.jpg",title:"x3",song:"x3.mp3"},
{src:"x4.jpg",title:"x4",song:"x4.mp3"}
as i do not have any java script experience , can somebody help me with this ? thanks in advance
Upvotes: 1
Views: 2221
Reputation: 10967
You have to expose that file from a web-server so your JavaScript can make an http request on that file.
To load resources from JavaScript you have to make an XMLHttpRequest or better known as AJAX request.
Actually this requires some setup so using a library would be easier. My favourite one is axios. It has a really simple API and handles response parse as well, so when you load axios on your web-site this is an approach you might follow :
axios.get('path-to-file').then(function(response){
this.testJson.list = response.data
});
Note that your x.txt does not seem like a valid JSON. It has to be a valid one so axios can parse it. If you decide to parse the file on your own you have to use JSON API.
Upvotes: 2