Javascript substitution for json

Greetings to all

I have a JSON code that looks like this:

{ playlist: [ 
    'URL goes here', 
    { 
        // our song 
        url: 'another URL goes here'
    }
  ]  
}

I would like to insert the value of the javascript variable in JSON and replace it with "URL here" instead. Is there any way to do this in JSON? I am noob in JSON, so help would be greatly appreciated. The value of the variable to replace will come from something like getElementById (). GetAttribute ().

Thanks NorthK

+3
source share
2 answers

So, I assume that you have a json object named jsonObject:

var url = "http://example.com/";
jsonObject.playlist[0] = url;

On the other hand, if you are talking about the construction of a json object, then you just put the variable in the right position:

var url = "http://example.com/";
var jsonObject = {playlist: [ 
    url, 
    { 
        // our song 
        url: 'another URL goes here'
    }
  ]  
}

, , url , , .

+9

, JSON Javascript Object Notation. , (, HTTP). , , : .

var newURL = = document.getElementById('foo').href; // or whatever...
myObject.playlist[0] = newURL;
+1

Source: https://habr.com/ru/post/1725365/


All Articles