0

json will not accept a variable storing a url after using a .replace() on that variable (myimg) as seen below:

$.get('http://api.soundcloud.com/resolve.json?url=https://soundcloud.com/'+user_name+'/tracks&client_id='+my_client_id, 
      function (result) {
        for (var i = 0; i < result.length; i++) {
            var myimg = result[i].artwork_url;
            var myimg = myimg.replace('-large', '-t500x500'); //problem line of code making the  variable not usable in json object below.

            trackdata[i] = {
                             title: result[i].title,
                             mp3: result[i].stream_url + '?client_id=' + my_client_id,
                             url: result[i].permalink_url,
                             poster: myimg, // not working if i use .replace() on myimg var above , works without it
                             sc: "true"
                           }
         }  

 for (i = 0; i < trackdata.length; i++) {
        playlist.push(trackdata[i]);
    } 

});

If I take out this line :

var myimg = myimg.replace('-large', '-t500x500');

The variable will work,however I would like to replace a string in the url before passing it to the json object. This will allow me to update the url to a higher quality image detailed in this question: Soundcloud 500x500 artwork by default

Community
  • 1
  • 1
user3741085
  • 99
  • 1
  • 2
  • 12
  • What does the content of myimg look like? By the way, you are creating a JS (not JSON) object. – ron tornambe Jun 27 '14 at 18:53
  • The contents of myimg is "[link]http://i1.sndcdn.com/artworks-000073227045-jvtbe6-large.jpg?2aaad5e[link]" for example – user3741085 Jun 27 '14 at 20:56
  • tried using regex replace? (btw you don't need the var keyword in that line) – e-MEE Jun 27 '14 at 21:18
  • No I have only tried the .replace() method. I have tried researching using regex to replace, however it not clear to me just yet. Is there a chance you could show me an example of how to apply regex replacing in this case? – user3741085 Jun 27 '14 at 22:15
  • Can you show the JSON object you are creating? – ron tornambe Jun 27 '14 at 23:18
  • I have added the line : playlist.push(trackdata[i]); inside a for loop above which is used to make javascript object which i called a json object. I still have not gotten this to work, I have also tried using regex var myimg = myimg.replace(/-large/, '-t500x500'); – user3741085 Jun 27 '14 at 23:24
  • 1
    Probably doesn't have anything to do with the problem, but you may want to remove the `var ` on that replace line. It's only needed for the first declaration of `myimg`. – primehalo Jun 27 '14 at 23:30

3 Answers3

0

Move

for (i = 0; i < trackdata.length; i++) {
  playlist.push(trackdata[i]);
}

Inside of your $.get, this looks like it is just a race condition (assuming the problem is "the image isn't showing up on the web page", and not a javascript error per se).

dave
  • 62,300
  • 5
  • 72
  • 93
  • Thank you, I do have it inside the .get in my code, I made a mistake adding it to my post. sorry. Still, i cannot use the replaced variable. If I console.log(myimg); I will see the replacement, but it is not sent to the javascript object. – user3741085 Jun 27 '14 at 23:36
  • What do you mean "it is not sent to the js object"? – dave Jun 27 '14 at 23:41
  • This code is to be used with for Jplayer, which builds a playlist based on songs in this format: `{ title:"Song Title", artist:"Song Artist", mp3:"song.mp3", oga:"song.ogg", poster: "img.png" }` after reading a json object it formats data an puts it in the format above using: `playlist.push(trackdata[i]); ` – user3741085 Jun 27 '14 at 23:52
0

I don't really understand what you mean by "works" and "doesn't work", but .replace should not have any effect at all in you case.

You should either move your last for loop inside the $.get or put it in a function and call that function inside $.get, because as it stands your for loop will get called (potentially) before your $.get call returns and your callback function is called. Could this be your problem?

$.get("...", function() {
  // you will reach here after you have a response

  // do stuff

  // run your for loop
}

// code here could get reached before you have a response from your get request

You should also use trackdata.push({...}); also make sure your arwork_url looks how you think it should look, perhaps add that to your js object and look at it alongside the poster property to get more information.

hnafar
  • 612
  • 8
  • 19
  • Thank you, I made a mistake adding the code to this post, i put ` for (i = 0; i < trackdata.length; i++) { playlist.push(trackdata[i]); } ` inside .get in my page. Still the variable is not being sent tot he javascript object if I run .replace on it. – user3741085 Jun 28 '14 at 00:01
0

Since I don't have a ClientId for SoundCloud, I am unable to test this in practice, but in theory something like this could achieve what I believe you are trying to do:

$.get('http://api.soundcloud.com/resolve.json?url=https://soundcloud.com/'+user_name+'/tracks&client_id='+my_client_id, 
    function (result) {
        var trackdata = [];
        result.forEach(function(the) {
            trackdata.push({
                title: the.title,
                mp3: the.stream_url + '?client_id=' + my_client_id,
                url: the.permalink_url,
                poster: the.artwork_url.replace('-large', '-t500x500'),
                sc: "true" // Hmm... why is this a string?
            });
        });
        // BTW, why the intermediate trackdata-array?
        trackData.forEach(function() {
            playlist.push(trackdata[i]);
        });
    });
Markku Uttula
  • 126
  • 1
  • 3
  • Thank you, I tried this code, it has not worked. SC : "true" is there because the code gets stream url's from a Soundclound. I got that line from tutorials such as this one: [link]http://efreedom.net/Question/1-15484352/Add-SoundCloud-Files-JPlayer[link] – user3741085 Jun 28 '14 at 15:55